【问题标题】:Simple HTML dom css selector not working简单的 HTML dom css 选择器不起作用
【发布时间】:2014-05-23 00:22:44
【问题描述】:

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

然而这什么也没有返回。

我的代码有什么问题?

【问题讨论】:

  • 虽然这根本不能回答问题,但使用 CSS 类比使用内联样式更好...

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


【解决方案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"]');

【讨论】:

  • 嗯。所以它实际上并不支持~= 选择器(同时支持一些非标准的选择器,例如[!attribute]!=)。这很奇怪。公平地说,Simple HTML DOM 的文档很烂——在再次查看文档后,我仍然无法弄清楚您在哪里找到了这张表。
  • @BoltClock How to find HTML elements? > Attribute Filters
【解决方案2】:

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

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

【讨论】:

  • hm 我试过 $dom->find('[style=~"font-family:symbol"]');它仍然没有返回任何内容
猜你喜欢
  • 2015-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多