简体   繁体   中英

Preg_match to find img src in specific img-tag

I have a line of sourcecode looking like this

 <img alt="this field is variable" title="this one too" itemprop="photo" border="0" style="width:608px;" src="imgurl.jpg">

There's lots of other images on the site, so i can't just preg_match all images, i need the specific one, i had a lot of trouble doing a specific preg_match, because content of the "alt"-tag and "title"-tag is variable. Anyone knows how to do so? Thanks in advance.

Itemprop="photo" is the thing unique for this picture.

This regex should work:

preg_match('/<img[^>]*itemprop="photo"[^>]*src="([^"]+)">/',$source,$matches);

An explanation of the regex (from regex101 ):

正则表达式的说明

The result will be in the array $matches .

Using regex to parse HTML is not a good thing . Why not use DOMDocument to search for your elements? PHP has these objects for parsing through an HTML document and examining elements much easier than using regex to try to find them. You would also then be able to manipulate the HTML much easier depending on what it is that you are trying to accomplish.

$dom = new DOMDocument();
$dom->loadHTML(<your html string>);

$imgs = $dom->getElementsByTagName('img');
$photos = [];
foreach($imgs as $img) {
      if($img->attributes->getNamedItem('itemprop') && $img->attributes->getNamedItem('itemprop')->nodeValue = 'photo') {
         $photos[] = $img->attributes->getNamedItem('src')->nodeValue;
     }
}

This code will get you an array with the src attribute of the imgs that have your property and you are not dependent on how the elements are created or anything in the actual text of the html.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM