2013-10-23 1 views
1

나는 또한 내가이 다음과 같은 형식으로 내가이 배열의 sample text 키워드를 찾으려면php를 사용하여 배열에서 키워드 찾기?

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

같은 있습니다. 내 예상 결과 이미 strpos을 시도하고 있지만이

답변

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); 

출력 : ~하지 내가 preg_grep에서 무엇을 의미하는지

Array 
(
    [0] => Canon sample printing text 
    [1] => Captain text 
    [3] => Fresh sample Roasted Seaweed 
    [4] => Fresh sample text Seaweed 
) 
+0

()? –

+0

'i'는 무시 대소 문자이고'~'는 정규식 구분 기호입니다. – anubhava

2

preg_grep 알려 주시기 바랍니다 정답

을받지

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 
    ) 

트릭을 수행합니다이 확실히 작동합니다

$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); 

희망을 당신.

관련 문제