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

时间:2014-04-09 16:32:31

标签: php css dom css-selectors simple-html-dom

参考http://simplehtmldom.sourceforge.net/

我有这段代码:

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);

尝试使用css选择器查​​找其样式中包含font-family:符号的标记。

然而,这没有任何回报。

我的代码出了什么问题?

2 个答案:

答案 0 :(得分:1)

请仔细阅读the doc ...可用的属性过滤器是:

+--------------------+----------------------------------------------------------------------------------------+
|       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.    |
+--------------------+----------------------------------------------------------------------------------------+

所以在你的情况下,你可以使用最后一个例子[以下是经过测试并且有效]:

$lis = $dom->find('[style*="font-family:symbol"]');

答案 1 :(得分:0)

您没有引用XPath中的字体内容。 find()调用应为

$lis = $dom->find('[style=~"font-family:symbol"]');
                           ^------------------^---- missing