PHP XML / HTML DOM使用空格获取CSS类属性 [英] PHP XML/HTML DOM get CSS class attribute with whitespace
问题描述
我遇到以下问题:
我使用PHP XML DOM解析器,当我解析现实世界的HTML时,很多元素的'class'属性都有空格,所以在实际上是这些元素的多个CSS类。但是,当我使用getAttribute()查询DOMNode时,我只得到第一个类。
I've got the following problem: I'm using the PHP XML DOM parser and when I'm parsing real-world HTML, the 'class' attributes of many elements have spaces in them, so there are actually multiple CSS classes for these elements. However, when I query the DOMNode with getAttribute(), I'm getting only the first class.
我也尝试过PHP simplehtmldom,但是相同的结果。
I have also tried PHP simplehtmldom for the very same purpose, but to the same result.
此问题之前已经被问过,但是没有真正的回答:
PHP dom来获取具有多个css类名的标签类
This question has been asked here before, however not really answered: PHP dom to get tag class with multiple css class name
此解决方法在我的情况下不起作用。任何帮助将不胜感激:)
This workaround does not work in my case. Any help would be appreciated :)
另一个问题包含一个非常好的例子,我需要完成,以防任何人不明白我。
The other question contains a very good example of what I need to accomplish in case anyone did not understand me.
推荐答案
你可以尝试这个例子在你链接到的stackoveflow问题。为我工作:
You can try this for the example in the stackoveflow question you linked to. Works for me:
$body = // the html
$html = new DomDocument();
$html->loadHTML($body);
$as = $html->getElementsByTagName('a');
foreach($as as $a) {
if ($a->getAttribute('class')=='secondLink SecondClass') {
// do something
}
}
这篇关于PHP XML / HTML DOM使用空格获取CSS类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!