简单的HTML dom css选择器不起作用


Simple HTML dom css selector not working

参考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:符号的标签。

然而,这不会带来任何回报。

我的代码出了什么问题?

请仔细阅读文档。。。可用的属性过滤器有:

+--------------------+----------------------------------------------------------------------------------------+
|       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"]');

您还没有在XPath中引用字体内容。find()调用应该是

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