0

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?

1
  • Although this doesn't answer the question at all, it would be better to use a CSS class than inline style... Commented Apr 9, 2014 at 16:35

2 Answers 2

1

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"]');
Sign up to request clarification or add additional context in comments.

2 Comments

Huh. So it doesn't actually support the ~= selector (while supporting some non-standard ones such as [!attribute] and !=). That's bizarre. To be fair, the documentation for Simple HTML DOM sucks - I still can't figure out where you found this table after looking at the documentation again.
@BoltClock How to find HTML elements? > Attribute Filters
0

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

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

1 Comment

hm I tried $dom->find('[style=~"font-family:symbol"]'); and it's still returning nothing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.