Use XPath with PHP's SimpleXML to find nodes containing a String?

It depends on what you want to do. You could select all the p elements that contain "Find me" in any of their descendants with xhtml:pcontains(., 'Find me') This will return duplicates and so you don't specify the kind of nodes then it will return body and html as well Or perhaps you want any node which has a child (not a descendant) text node that contains "Find me text()contains(., 'Find me') This one will not return html or body I forgot to mention that represents the whole text content of a node text() is used to retrieve a nodeset of text nodes. The problem with your expression contains(text(), 'Find me') is that contains() only works on strings, not nodesets and therefore it converts text() to the value of the first node, which is why removing the first br makes it work.

It depends on what you want to do. You could select all the elements that contain "Find me" in any of their descendants with //xhtml:pcontains(., 'Find me') This will return duplicates and so you don't specify the kind of nodes then it will return and as well. Or perhaps you want any node which has a child (not a descendant) text node that contains "Find me" //*text()contains(., 'Find me') This one will not return or .

I forgot to mention that . Represents the whole text content of a node. Text() is used to retrieve a nodeset of text nodes.

The problem with your expression contains(text(), 'Find me') is that contains() only works on strings, not nodesets and therefore it converts text() to the value of the first node, which is why removing the first makes it work.

Thanks, that one works! – xl-t Sep 16 '10 at 12:14.

$doc = new DOMDocument(); $doc->loadHTML($xhtml); $xPath = new DOMXpath($doc); $xPathQuery = "//text()contains(translate(.,'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 'Find me')"; $elements = $xPath->query($xPathQuery); if($elements->length > 0){ foreach($elements as $element){ print "Found: " . $element->nodeValue. ""; }}.

But thanks @Jordy for the quick answer. First, that's DOM-XML, which is not desired, since everything else in my script is done with SimpleXML. Second, why do you translate to uppercase and search for an unchanged string 'Find me'?

'Searching for 'FIND ME' would actually give a result. But you pointed me towards the right direction: $nodes = $xml->xpath("//text()contains(., 'Find me')"); does the trick!

Then vote my answer as the answer :D The translate for uppercase is when you want to make it non-case sensitive – Jordy Sep 16 '10 at 12:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions