Search code examples
xmldomxpathdomdocument

DOMXPath: Find all H2 and H3 headlines with a specific character


I want to find every <h2> and <h3> tags which are a question. So I need to check if the headline contains a question mark ?.

At the moment my code looks like this:

$query = '//*[contains("h2 h3", name())]';

I saw in this answer, that there is some kind of selector. But I'm not sure how to use something like this with my h2 and h3 selectors:

$query = '//*[contains(text(), '?')]';

Here I found this:

$query = '//h2/*[contains(text(), "?")]';

But I get an error in my function. You can see the function here. I'm not sure if that's related.


Solution

  • I found a solution:

    $query = "//h2[contains(., '?')] | //h3[contains(., '?')]";