简体   繁体   English

Preg_match在特定的img标签中查找img src

[英]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. 该网站上还有很多其他图片,因此我不能只preg_match所有图片,我需要特定的图片,我在进行特定的preg_match时遇到很多麻烦,因为“ alt”标签和“ title”的内容标签是可变的。 Anyone knows how to do so? 有人知道怎么做吗? Thanks in advance. 提前致谢。

Itemprop="photo" is the thing unique for this picture. Itemprop =“ photo”是此图片唯一的东西。

This regex should work: 这个正则表达式应该工作:

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

An explanation of the regex (from regex101 ): 正则表达式的解释(来自regex101 ):

正则表达式的说明

The result will be in the array $matches . 结果将在数组$matches

Using regex to parse HTML is not a good thing . 使用正则表达式解析HTML不是一件好事 Why not use DOMDocument to search for your elements? 为什么不使用DOMDocument搜索元素? PHP has these objects for parsing through an HTML document and examining elements much easier than using regex to try to find them. PHP具有这些对象,可以通过HTML文档进行解析并检查元素,这比使用正则表达式尝试查找它们要容易得多。 You would also then be able to manipulate the HTML much easier depending on what it is that you are trying to accomplish. 然后,您还可以根据要完成的操作更轻松地操作HTML。

$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. 此代码将让你用的src属性的阵列imgs有你的财产,你是不依赖于如何创建或HTML的实际文本的任何元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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