php domdocument check span class [英] php domdocument check span class
问题描述
如何迭代所有标签,检查class是font18还是font17?
How can I iterate all tag and check if class is font18 or font17?
$html = new DOMDocument();
$html->load('file.html');
html:
<p><a name="bookmark7"></a><span class="font18" style="font-weight:bold;">Abilitazione</span></p>
<p><span class="font17">I medici devono essere autorizzati dallo Stato a praticare la loro professione. I requisiti per ottenere questa autorizzazione variano a seconda delle diverse Nazioni. I laureati presso Facoltà mediche estere possono ottenere l'autorizzazione a esercitare in Italia se rispondono ai requisiti statali per quanto riguarda il tirocinio e se superano l'esame di Stato. Nell'ambito della CEE si tratta tuttora di una questione da definire nei particolari.</span></p>
非常感谢。
推荐答案
下面将循环遍历所有 span
标签,您可以使用它来检查类(如果您提供的HTML代码片段确实是您正在使用):
The follwing will loop through all span
tags and you can use this to check the class (if the HTML snippet you provided is indeed the one you are using):
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->load('file.html');
$xpath = new DOMXPath($doc);
$nodes = $xpath->query('//span');
foreach ($nodes as $node) {
echo $node->getAttribute('class');
}
演示: http://codepad.viper-7.com/pQuQw1
如果HTML实际上不同可以告诉我,所以我可以改变我的片段。也可以仅选择xpath查询中的特定元素(例如,仅选择类 font17
或 font18
)。
If the HTML is actually different you can tell me so I can change my snippet. It may also be worthwhile to only select specific elements in the xpath query (e.g. to only select elements with class font17
or font18
) .
请注意,我已经使用DOMXPath,因为这将使您更灵活地更改查询以根据您的HTML
如果您只想选择类为 font17
或 font18
您可以将查询更改为:
If you only want to select elements with class font17
or font18
you can change the query to something like:
$nodes = $xpath->query('//span[contains(@class, "font17")]|//span[contains(@class, "font18")]');
演示: http://codepad.viper-7.com/mHo5P7
这篇关于php domdocument check span class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!