سؤال

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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

You haven't quoted the font stuff in your XPath. The find() call should be

$lis = $dom->find('[style=~"font-family:symbol"]');
                           ^------------------^---- missing
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top