0

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')

halfer
  • 19,824
  • 17
  • 99
  • 186
  • @Toto i have see the link u add tried with it and its not work for me. I need preg_grep expression that search those array elemets that contains charcter from specific set with any order, but your given link solution work with fixed order pattern i.e., 'php' – Vaswati Pal May 11 '17 at 12:22
  • I was just about to type in my answer! (not a regex one) – mickmackusa May 11 '17 at 12:30
  • @Toto that question is looking for literally ordered `php` this question is looking for `ent` or `net` or `ten` etc. – mickmackusa May 11 '17 at 12:32
  • 1
    Why is the variable name identical in the other question? Is this homework or something? – mickmackusa May 11 '17 at 12:33
  • If you ask a new question seeking the same fitlered output but NOT using regex/preg_grep, then I have produced an answer for you. ...what an irritating closure. – mickmackusa May 11 '17 at 12:35
  • its just co-incident, of same variable name and its not a part of home work is a part of live job. currently i try to develop a word scrabble program – Vaswati Pal May 11 '17 at 12:35
  • Is a non-regex solution okay for you? I tried to write a regex pattern, but it was all getting to convoluted. Will you ask a new question not requiring regex? I'll have to find the new question if you do. – mickmackusa May 11 '17 at 12:37
  • if u help me with finding those array element have contain 'ent', 'net', 'ten' with also those words that have single or double charcter before or after it i.e., 'sten' , 'netff' etc then i have no issue – Vaswati Pal May 11 '17 at 12:44
  • Getting your question re-opened is probably going to take a while. Perhaps ask a new question with different criteria (not requiring regex) to filter your `$findgroup` array. – mickmackusa May 11 '17 at 12:45
  • If u find any solution plz let me know. I am stuck on that issue for long time – Vaswati Pal May 11 '17 at 12:51
  • I'm telling you I have a working solution. I spent about an hour thinking/developing/testing it, so I kind of what to get some points out of it. If you start up a new question, I'll answer it while I am still awake. – mickmackusa May 11 '17 at 12:53
  • [link](http://stackoverflow.com/questions/43916223/find-element-of-an-array-those-contains-only-specific-character-set-in-php) check this one plz – Vaswati Pal May 11 '17 at 13:01

0 Answers0