Example:
$array = array('alpha beta','beta gamma','delta phi', '#alpha phi', 'beta phi');
$searchword = 'alpha';
$results = array_filter($array, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", $var); });
print_r($results);
Array ( [0] => alpha beta [3] => #alpha phi )
I would like to find only the elements containing #alpha
, not alpha
.
The result I want is Array ( [3] => #alpha phi )
.
However, this doesn't work:
$array = array('alpha beta','beta gamma','delta phi', '#alpha phi', 'beta phi');
$searchword = '#alpha';
$results = array_filter($array, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", $var); });
print_r($results);
Array ( )
I also tried $searchword = preg_quote('#alpha');
but that doesn't work either. I'm not familiar enough with regex to figure this out-- there must be some regex rule I'm missing out on?
Note: I am not looking to find all the hashtag keywords in the array. I want to search for a specific hashtag keyword.
Credit: I used one of the answers from here: Search for PHP array element containing string