There is an array of the form:

array(3) { [0]=> string(*) "test.ru-111.11111.111" [1]=> string(*) "site2.ru-2222.22.222" [2]=> string(*) "test.in-333.333.333" } 

I need to find a value in it (more precisely, the number under which this value is written in the array), for example, test.ru , I do this:

 array_search('test.ru', $array); 

does not find. A

 array_search('test.ru-111.11111.111', $array); 

finds

But it is clear why ... How to make the word that comes to - .

Provided that the $array cannot be output in a loop.

//////////////////////////

 foreach($new_arr as $list_d_ip) { $iskomii = explode('-', $list_d_ip); $iskomii = str_replace(' ', '', $iskomii[0]); $patt = '/'.$iskomii.'-/'; $ss = preg_grep($patt, $array); var_dump($ss); } 
 array(3) { [0]=> string(26) "zesiduwyk.ru -11.11.11.11 " [1]=> string(27) "edeziragaj.ru -2222.222.22 " [2]=> string(13) "test.ru-00000" } 

    1 answer 1

    Use preg_grep() to search in an array using a regular expression.

     $array = [ "test.ru-111.11111.111", "site2.ru-2222.22.222", "test.in-333.333.333", ]; var_dump(preg_grep('/test.ru-/', $array)); # Вывод: array(1) { [0]=> string(21) "test.ru-111.11111.111" } 
    • ^ wouldn’t hurt to add to the beginning - teran
    • @teran, hmm, to the beginning of a regular expression? Honestly, I almost don’t work with them, I don’t know the details. - entithat
    • for some reason only the last element is found. I have an array in which the domains that need to be found are written, output so foreach($new_arr as $list_d_ip) { var_dump(preg_grep('/'.$list_d_ip.'-/', $array)); } foreach($new_arr as $list_d_ip) { var_dump(preg_grep('/'.$list_d_ip.'-/', $array)); } - iKey
    • '/'.$list_d_ip.'-/' , try '/'.$list_d_ip.'-/' change to - "/$list_d_ip-/" , or make the string search template as a separate variable. - entithat
    • does not help ... how to be? - iKey