Good time! I’m an idle task, but I’m not strong yet in drafting regulars, please help.

there is a string with img tags

$str = "<img src='image1.jpg'> <img src='image23.jpg'><img src='image3.jpg'>"; 

It is necessary to break it into an array of variables. I use as a delimiter this> <the beginning of one tag and the end of another, while taking into account that between> and <there may be a random number of spaces or not one at all (and maybe tabulation) tried this way and for example:

 $seal = preg_split("/>.+?</",$str); 

but I have from the line where there are only 3 tags img my variations give crazy results when I check the result through the count then 1 then 62 then 70 and so on. But I can’t get the real value

  • And what is the essence of the task that you need to get? Parsing html regular expressions is not a good idea, it is better to use domDocument - jekaby

2 answers 2

 preg_match_all('/<.+?>/', $str, $result); $seal = $result[0]; 

    I offer you the option of not separating, but selecting (capturing into an array) all tags:

     preg_match_all('~(<img src=\'[a-z0-9\.]+\'>)~u', $str, $matches); 

    For the result, see $ matches [1]:

     print_r($matches[1]);