I need to find any character before or after of a word using PHP preg_grep() from array. I have a array like following
preg_grep()
$findgroup = array(\"aphp\", \"
Anchor your regex and add quantifier for character before and after:
$findgroup = array("aphp", "phpb", "dephpfs", "potatoes", "aphpb", "php"); $result = preg_grep("/^(?:.{1,2}php|php.{1,2})$/", $findgroup); print_r($result);
Output:
Array ( [0] => aphp [1] => phpb )