【问题标题】:Find all occurences of an HTML tag in PHP [closed]在 PHP 中查找所有出现的 HTML 标签 [关闭]
【发布时间】:2015-08-15 15:09:41
【问题描述】:

我怎样才能找到所有出现的 html 标记?例如,如果我的字符串是...

<h1>Images</h1>
<img src="img/bird.jpg">
<p>Hello world</p>
<img src="img/beach.jpg" alt="test" class="picture">

如果我想列出所有 IMG 标签,它会是这样的:

<img src="img/bird.jpg">
<img src="img/beach.jpg" alt="test" class="picture">

你们会怎么做呢?

【问题讨论】:

标签: php html


【解决方案1】:

你可以从这里http://simplehtmldom.sourceforge.net/使用简单的html dom库

您可以通过以下方式找到所有 img 标签:

<?php

include('simple_html_dom.php');

$html = str_get_html('<h1>Images</h1>
<img src="img/bird.jpg">

<p>Hello world</p>
<img src="img/beach.jpg" alt="test" class="picture">');

foreach($html->find('img') as $img) {
    print $img->outertext . PHP_EOL;
}

【讨论】:

  • 这可能是最简单的选择。但是,有没有办法在没有库的情况下做到这一点?
  • 是的,请参阅@FuzzyTree 的评论
  • 用完这个,我就用这个。虽然如果您不想麻烦安装,@FuzzyTree 的评论将是最好的选择。
猜你喜欢
  • 2023-04-01
  • 1970-01-01
  • 2012-02-08
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 2016-06-11
相关资源
最近更新 更多