【问题标题】:PHP Simple HTML Dom - tag attribPHP Simple HTML Dom - 标签属性
【发布时间】:2013-01-17 05:36:37
【问题描述】:

我正在尝试从<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();

?>

【问题讨论】:

    标签: php simple-html-dom


    【解决方案1】:

    我不知道 Simple_html_dom。但是您尝试传递的查询似乎是一个 xpath 查询。在这种情况下,您需要使用带有@ 的前缀属性。此外,您需要在整个查询前加上 // 以确保它搜索任何深度级别的 font 标记。最终查询应如下所示。

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

    使用 DOMDocument 和 DOMXPath it works pretty good

    $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";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 1970-01-01
      相关资源
      最近更新 更多