douxi3432 2014-04-09 16:32
浏览 15
已采纳

简单的HTML dom css选择器无法正常工作

in reference to http://simplehtmldom.sourceforge.net/

I have this code:

require_once('simple_html_dom.php');

$x = '<span style="font-family:symbol">&#174;</span>';
$dom = str_get_html($x);
$lis = $dom->find('[style=~font-family:symbol]');
print_r($lis);

Which tries to find tags whose style has the font-family:symbol in it using a css selector.

However this returns nothing.

What is wrong with my code?

  • 写回答

2条回答 默认 最新

  • douji8549 2014-04-09 17:54
    关注

    Please read the doc carefully... The available attributes filter are :

    +--------------------+----------------------------------------------------------------------------------------+
    |       Filter       |                                      Description                                       |
    +--------------------+----------------------------------------------------------------------------------------+
    | [attribute]        | Matches elements that have the specified attribute.                                    |
    | [!attribute]       | Matches elements that don't have the specified attribute.                              |
    | [attribute=value]  | Matches elements that have the specified attribute with a certain value.               |
    | [attribute!=value] | Matches elements that don't have the specified attribute with a certain value.         |
    | [attribute^=value] | Matches elements that have the specified attribute and it starts with a certain value. |
    | [attribute$=value] | Matches elements that have the specified attribute and it ends with a certain value.   |
    | [attribute*=value] | Matches elements that have the specified attribute and it contains a certain value.    |
    +--------------------+----------------------------------------------------------------------------------------+
    

    So in your case, you can use the last one for example [the following is tested and it works]:

    $lis = $dom->find('[style*="font-family:symbol"]');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?