Find element of an array those have only specific character set using preg_grep() in php

38 views Asked by At

I need to find only those elements of an array that have specific set of letters and any character before or after of finds word using PHP preg_grep() from array.

I have an array like following $findgroup = array("sten", "netff", "enet", "denet");

I need to find the values from the array which have 'e,n,t' characters and single or double character before or after(either side, not both side) the match word. If i search with pattern e,n,t and 1 letter before or after(either side, not both side) it the result will be array("sten", "enet") and if i search with pattern e,n,t and 2 letter before or after(either side, not both side) it the result will be array("netff", "denet") I tried with the following code but not works.

1 letter before or after(either side, not both side):

$result = preg_grep("/^(?:.{1}".$value."|".$value.".{1})$/", $findgroup);

2 letter before or after(either side, not both side):

$result = preg_grep("/^(?:.{2}".$value."|".$value.".{2})$/", $findgroup);

Here $value is my search pattern array i.e., array('e', 'n', 't')

0

There are 0 answers