FIA2008 FIA2008 - 5 months ago 17
PHP Question

XPath query for content between HTML tags

I have HTML like this:

<html>
<head>
<meta content="{{find_this}}" />
</head>
<body>
<div>{{find_this}}</div>
<span>{{find_this}}</span>
<p>{{find_this}}</p>
</body>
</html>


I load this in a DOM document and XPath like this:

$dom = new DOMDocument();
$dom->loadHTML($template);
$xpath = new DOMXPath($dom);


I want to use xpath to query a tag that has
{{find_this}}
as value and no as attribute value. How can I do this?

Answer

This XPath expression,

//*[.='{{find_this}}']

will select all elements (not attributes) whose string value equals {{find_this}}.

Alternatively, if you wish to select all elements whose string value contains the string, use this XPath:

//*[contains(.,'{{find_this}}')]