Find character before or after of string using preg_grep() in PHP

前端 未结 1 1407
走了就别回头了
走了就别回头了 2021-01-28 15:25

I need to find any character before or after of a word using PHP preg_grep() from array. I have a array like following

$findgroup = array(\"aphp\", \"

相关标签:
1条回答
  • 2021-01-28 16:30

    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
    )
    
    0 讨论(0)
提交回复
热议问题