Jeff
Jeff

Reputation: 4003

php xpath query with contains and regex

Right now I have a xpath search function that looks like this:

$paragraph = $xmldoc->xpath("//p[contains(., '$wordsearch')]");

I was wondering if it is possible to let $wordsearch be a regular expression, so that my search looked something like this:

$paragraph = $xmldoc->xpath("//p[contains(., '$regularExpression')]");

Thanks for your help.

Upvotes: 0

Views: 1058

Answers (1)

hakre
hakre

Reputation: 197842

You can filter the array with the regex instead:

$paragraph = array_filter(
    $xmldoc->xpath("//p"), 
    function ($p) use ($regularExpression) {
        return preg_match($regularExpression, $p);
    }
);

See array_filter.

Upvotes: 1

Related Questions