Preg_match_all, get all img tags that contain the string

this code gets all img tags

preg_match_all('/<img[^>]+>/i',$a,$page);

but I want to get tags so their file names include "next.gif" or "pre.gif"

eg:

$page = '
<img border="0" alt="icon" src="http://www.site.com/images/man.gif" width="90" height="90">
<img border="0" alt="icon" src="http://www.site.com/images/pre.gif" width="90" height="v">
<img border="0" alt="icon" src="http://www.site.com/images/2.gif">
<img border="0" alt="icon" src="http://www.site.com/images/next.gif" width="90" height="90">
';

and I should be as follows:

   <img border="0" alt="icon" src="http://www.site.com/images/pre.gif" width="90" height="90">
    <img border="0" alt="icon" src="http://www.site.com/images/next.gif" width="90" height="90">
+3
source share
1 answer

I need to go with this:

/(<img[^>]*src=".*?(?:pre\.gif|next\.gif)"[^>]*>)/i

Or in PHP:

$regexp = '/(<img[^>]*src=".*?(?:pre\.gif|next\.gif)"[^>]*>)/i';
$iResults = preg_match_all($regexp, $str, $aMatches);
print_r($aMatches); // you'll see what you need

- change: Sorry. I made a mistake. .in pre.gifand next.gifin regexp the regex must be escaped !! I didn’t do this before. - edit

PS. You may be using preg_match_all incorrectly. Arguments are: ( pattern, subject, &matches)

PS. My template results + your item:

Array
(
    [0] => Array
        (
            [0] => <img border="0" alt="icon" src="http://www.site.com/images/pre.gif" width="90" height="v">
            [1] => <img border="0" alt="icon" src="http://www.site.com/images/next.gif" width="90" height="90">
        )
    [1] => Array
        (
            [0] => <img border="0" alt="icon" src="http://www.site.com/images/pre.gif" width="90" height="v">
            [1] => <img border="0" alt="icon" src="http://www.site.com/images/next.gif" width="90" height="90">
        )
)
+1

Source: https://habr.com/ru/post/1782035/


All Articles