preg_match_all multi word search

Wednesday, November 4, 2015

Basically I'm trying to use preg_match_all in order to perform a multi search.
I have 3 arrays :
- $searchWordBis, which contains the words to be searched.
- $linkArray, which contains strings, that's the array I want to search in.
- $matches, which will contain the results of the research.



I want each word of $searchWordBis to be searched in $linkArray.



Here is what I wrote :
for ($i = 0; $i <= count($searchWordBis)-1 ; $i++) { //loop test to do the search for each word of $searchWordBis and that way opperate a multi-words search



        $fct = function($var) use ($searchWordBis) { //$fct is a "closure" (anonymous function)
return preg_match_all("/\b$searchWordBis[$i]\b/i", $var, $pregMatches);
};
$matches = array_values(array_filter($linkArray, $fct)); //filtering $matches array according to $fct + reseting index

/* Tests */
echo $searchWordBis[$i];
echo '<br>';

echo '<pre>';
var_dump($pregMatches);
echo '</pre>';

echo '<pre>';
var_dump($matches);
echo '</pre>';
/* ----- */
}


But it isn't working. What's wrong in my code ? Thanks

0 comments:

Post a Comment