PHP简单HTML Dom标签属性


PHP Simple HTML Dom - tag attrib

我正在尝试从<font size="3" color="blue">内抓取纯文本…它没有捡起字体标签,虽然它确实工作,如果我做"font", 3,但有很多字体标签在网站上,我想使搜索更具体一点。有可能在一个标签上有多个属性吗?

<?php
include('simple_html_dom.php');
$html = new simple_html_dom();   
$html = file_get_html('http://cwheel.domain.com/');
##### <font size="3" color="blue">Certified Genuine</font>
$element = $html->find("font[size=3][color=blue]", 0);  
echo $element-> plaintext . '<br>';
$html->clear();
?>

我不知道Simple_html_dom。但是,您试图传递的查询似乎是xpath查询。在这种情况下,您需要使用@的前缀属性。此外,您需要用//作为整个查询的前缀,以确保它搜索任何级别深度的任何font标记。最后的查询应该是这样的:

//font[@size=3][@color=blue]

使用DOMDocument和DOMXPath,它工作得很好。

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$fonts = $xpath->query('font[@size="3" ][ @color="blue"]');
foreach($fonts as $font){
    echo $font->textContent. "'n";
}