0

How would one search through a HTML document to find the JavaScript myfunc within it? That is, I'd like to find the aforementioned needle in an HTML haystack that may contain the following nodes.

  • <p onMouseOver="myfunc(a,b,c)">blah blah blah</p>
  • <a href="javascript:MyFunc(a, b, c)">blah blah blah</a>
  • <img onmouseover='myFunc(a,b, c )' src="someimage.gif">

The PHP code I am currently using is:

foreach($domxpath->query('[onmouseover^="myfunc"]') as $node) {
    $text = $node->nodeValue;
    echo "<br>text=".$text;  // just to see what i get
}
citizen
  • 353
  • 3
  • 13
  • Big ups to [Explosion Pills](http://stackoverflow.com/users/454533/explosion-pills) for correcting my question. – citizen Feb 21 '13 at 04:07

2 Answers2

1

You want a pattern like //*[contains(@onmouseover, 'myfunc')]:

  • //* selects all elements in the document.
  • [contains(@onmouseover, 'myfunc')] then selects those for which the onmouseover attribute contains "myfunc".
ajshort
  • 3,684
  • 5
  • 29
  • 43
0

You're not just searching for onmouseover it seems (look at the <a>). Hopefully the function name is consistent because PHP is only XPath 1.0

//*[@*[contains(.,"myfunc")]]

This selects any node anywhere with any attribute that contains "myfunc" (case sensitive).

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • How would I make this case _insensitive_? – citizen Feb 21 '13 at 04:07
  • Try with XPath 2.0 it would be so easy, but I'm afraid there is no simple case insensitive search available. There is an ugly alternative, though: http://stackoverflow.com/questions/3238989/case-insensitive-xpath-searching-in-php – Explosion Pills Feb 21 '13 at 04:44
  • Pardon, but the solution gives me the node name & value. How do I access the attribute? [getAttribute](http://www.php.net/manual/en/domelement.getattribute.php) requires the attribute name to be known. – citizen Feb 21 '13 at 15:42
  • You can scan `$node->attributes` and do a similar comparison (you've narrowed down the nodes). If you want to select the attribute node itself, you can use `//@[contains(.,"myfunc")]]` I think – Explosion Pills Feb 21 '13 at 15:48