0

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.

4
  • It's not supported, because in PHP the underlying library only implements XPath 1.0. Commented Jan 21, 2013 at 1:47
  • erg, php is letting me down Commented Jan 21, 2013 at 1:55
  • you can filter the return array with the regex instead. Commented Jan 21, 2013 at 2:16
  • Reference: Using regex to filter attributes in xpath with php (Jul 2011) Commented Aug 17, 2015 at 5:31

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.