1

我有一个类似这种格式的关键字

sample text

我也有一个这样的数组,格式如下

Array
(
   [0] => Canon sample printing text
   [1] => Captain text
   [2] => Canon EOS Kiss X4 (550D / Rebel T2i) + Double Zoom Lens Kit
   [3] => Fresh sample Roasted Seaweed
   [4] => Fresh sample text Seaweed
)

我想sample text在这个数组中找到关键字。我的预期结果

Array
    (
       [0] => Canon sample printing text        //Sample and Text is here
       [1] => Captain text             //Text is here
       [3] => Fresh sample Roasted Seaweed       //Sample is here
       [4] => Fresh sample text Seaweed          //Sample text is here
    )

我已经在尝试strpos但没有得到正确的答案

请指教

4

2 回答 2

2

preg_grep可以解决问题:

$input = preg_quote('bl', '~'); // don't forget to quote input string!
$data = array('orange', 'blue', 'green', 'red', 'pink', 'brown', 'black');

$result = preg_grep('~' . $input . '~', $data);

希望这对你有用。

于 2013-10-23T09:03:05.673 回答
2

一个简单preg_grep的就可以完成这项工作:

$arr = array(
    'Canon sample printing text',
    'Captain text',
    'Canon EOS Kiss X4 (550D / Rebel T2i) + Double Zoom Lens Kit',
    'Fresh sample Roasted Seaweed',
    'Fresh sample text Seaweed'
);
$matched = preg_grep('~(sample|text)~i', $arr);
print_r($matched);

输出:

Array
(
    [0] => Canon sample printing text
    [1] => Captain text
    [3] => Fresh sample Roasted Seaweed
    [4] => Fresh sample text Seaweed
)
于 2013-10-23T09:03:12.613 回答