【问题标题】:How to use preg_match_all to obtain all images of a post如何使用 preg_match_all 获取帖子的所有图像
【发布时间】:2012-07-12 14:26:30
【问题描述】:

我有一个博客条目,其中包含多张图片(有时一张,有时两张,有时三张),看起来有点像这样:

<a href="http://xxx/" rel="attachment w133>
  <img class="yyy" title="title1" src="http://xxx/title1.jpg" alt="" width="650" height="487" />
</a>
<a href="http://xxx/" rel="attachment w134">
  <img class="yyy" title="title2" src="http://xxx/title2.jpg" alt="" width="650" height="487" />
</a>
<a href="http://xxx/" rel="attachment w135">
  <img class="yyy" title="title3" src="http://xxx/title3.jpg" alt="" width="650" height="487" />
</a>

后面有一些文字。

现在,我想知道如何使用 preg_match_all 来提取第一部分。我现在对 PHP 编程有所了解,但从未使用过 preg_match_all。

这里的代码确实只提取最后一张图片,这还不够:

$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post_content, $matches);

如果可能的话,如果有人能给我一个提示如何实现这一点,那就太好了。非常感谢!

【问题讨论】:

  • .将匹配任何字符,因此请尝试 [^] 以使其保留在图像标签内
  • 使用HTML parser 而不是正则表达式可能会更好。然后您可以使用 XPath 查询获取图像。
  • @Waygood。谢谢。也许如果我会使用类似的东西: ('/') 它会提取这些东西?

标签: php regex image extract preg-match-all


【解决方案1】:
$post_content='<a href="http://xxx/" rel="attachment w133>
  <img class="yyy" title="title1" src="http://xxx/title1.jpg" alt="" width="650" height="487" />
</a>
<a href="http://xxx/" rel="attachment w134">
  <img class="yyy" title="title2" src="http://xxx/title2.jpg" alt="" width="650" height="487" />
</a>
<a href="http://xxx/" rel="attachment w135">
  <img class="yyy" title="title3" src="http://xxx/title3.jpg" alt="" width="650" height="487" />
</a>
';

preg_match_all('/<a\s[^>]*href=([\"\']??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU', $post_content, $matches);
//print_r ($matches);//$matches - array which contains all your images
print $matches[0][0]; //first link with image
print $matches[0][1]; //second link with image
print $matches[0][2]; //third link with image

输出:

<a href="http://xxx/" rel="attachment w133&gt;
  &lt;img class=" yyy"="" title="title1" src="http://xxx/title1.jpg" alt="" width="650" height="487">
</a>
<a href="http://xxx/" rel="attachment w134">
  <img class="yyy" title="title2" src="http://xxx/title2.jpg" alt="" width="650" height="487">
</a>
<a href="http://xxx/" rel="attachment w135">
  <img class="yyy" title="title3" src="http://xxx/title3.jpg" alt="" width="650" height="487">
</a>

【讨论】:

  • 和上面说的一样,不是吗?结果如下: Array ( [0] => Array ( [0] => xxx/title1.jpg" alt="" width="650" height="487 " />xxx" rel="">xxx/title2.jpg" alt="" width="650" height= "487" />xxx" rel="">xxx/title3.jpg" alt="" width="650" height="487" /> ) [1] => 数组 ([0] => xxx/title2.jpg )
  • 这不是我想要的,因为它只需要最后一张图像。然后,它也没有得到 。但是不确定我是否需要它们。
  • 太棒了!极好!非常感谢
  • 但是,再次检查,它似乎不包括所有照片/图像。例如:codexxx/Paradiese.002.png" rel="shadowbox" title="Paradiese.002">xxx/Paradiese.002-1024x806.png" alt="" title="Paradiese.002" width="650" height="511" class="alignleft size-large wp-image-316" />
  • 啊,哦,我的错。代码中有一个小小的拼写错误。效果很好!再次感谢!
【解决方案2】:

试试:

preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"][^\/>]*>/Ui', $post_content, $matches);
print_r($matches);

【讨论】:

  • 这只会导致:“Array ( [0] => Array ( ) [1] => Array ( ) )”
猜你喜欢
  • 2015-05-10
  • 2015-03-23
  • 2011-09-16
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
相关资源
最近更新 更多