简体   繁体   English

PHP:简单的HTML DOM解析器 - 如何获取具有特定标记名称的元素?

[英]PHP: Simple HTML DOM Parser - how to get the element which has certain tag name?

In PHP I'm using the Simple HTML DOM Parser class. 在PHP中,我使用的是Simple HTML DOM Parser类。

I have a HTML file which has multiple and diferents tags. 我有一个HTML文件,它有多个和不同的标签。

In this HTML there is an element like this: 在这个HTML中有一个这样的元素:

<a name="10418"><b>&nbsp;Hospitalist (Family Practitioner)</b></a>

So I would like to find that ' a ' element with has name="10418" 所以我想找到名字=“10418”的 ' a '元素

I've tried this with no luck, because I only want to get that string. 我试过这个没有运气,因为我只想得到那个字符串。

  $html_object = str_get_html($url);
  $html_object=$html_object->find('a');
  foreach ($html_object as $o) {
            $a= $o->find("b");
            echo $a[0];
 }

Try: 尝试:

$anchor = $html_object->find('a[name=10418]', 0);
echo $anchor->plaintext;

Working DEMO 工作演示

Try another library called Tag Parse .It's simple and efficient. 尝试另一个名为Tag Parse的库。它简单而有效。

$dom = new TagParse\TagDomRoot($html);
$a = $dom->find('a[name="10418"]');

I think it's fast and cost less memory than simple_html_dom. 我认为它比simple_html_dom更快,成本更低。

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

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