$array = ['GETST', 'GETT', 'GET', 'POST', 'GPOST', 'GEPOST', 'GETOST']; preg_grep('/GET|POST/', $array); 

All the specified array elements pass this regular expression. How to rewrite the regular season so that only GET and POST can pass?

1 answer 1

To limit matches in a pattern, you can use a sequence of characters \b...\b , denoting a word boundary , and not preserving brackets (?:...) :

 $array = ['GETST', 'GETT', 'GET', 'POST', 'GPOST', 'GEPOST', 'GETOST']; $result = preg_grep('~\b(?:GE|POS)T\b~', $array); echo '<pre>', print_r($result, true), '</pre>'; 

Result:

 Array ( [2] => GET [3] => POST )