2

I need to know if a particular string exist in a particular node. For example, I need to know if "quick brown fox" exist in the, say, 3rd paragraph of an HTML DOM. I'm using PHP's DOMXPath. Any suggestions?

StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441

1 Answers1

5

Try the following:

Example source:

$html = <<< HTML
<body>
    <p>the dead brown fox</p>
    <p>the slow brown fox</p>
    <p>the quick brown fox</p>
    <p>the crawling brown fox</p>
</body>
HTML;

Code:

$dom = new DOMDocument;
$dom->loadXml($html);
$xp = new DOMXPath($dom);
echo $xp->evaluate('count(/body/p[3][contains(., "quick")])');

The XPath translates to count the 3rd p element below the body element that contains a text node value of "quick". This will return 1 or 0 if the searched term exists anywhere within the node value.

If you need to know if the node value starts with a specific phrase, use the starts-with function instead.

PHP's DOM extension supports XPath 1.0.

You can also do it without XPath through the regular API:

$dom = new DOMDocument;
$dom->loadXml($html);
$thirdPara = $dom->getElementsByTagName('p')->item(2);
echo strpos($thirdPara->nodeValue, 'the quick') === 0 ? 1 : 0;

The getElementsByTagName method finds, who would have thought, all elements by tag name. The item call returns the third of those elements (zero-based). The strpos function finds the position of first occurrence of a string. The result of the snippet above would be 1 again (or 0 if the node value does not start with 'the quick'.

Gordon
  • 312,688
  • 75
  • 539
  • 559