Using PHP in XPath

/ Published in: PHP
Save to your folder(s)

I'm using this out of laziness.
PHP is slowing things down here, since it's not as close to C as XPath.
A better solution would be to have an attribute specifically for search purposes (either in upper or lowercase).

Copy this code and paste it in your HTML
  1. $xpath = new DOMXPath($dom);
  2. if (method_exists($xpath, "registerPHPFunctions")) {
  3. $xpath->registerNamespace("php", "");
  4. $xpath->registerPHPFunctions('strtolower');
  5. $query = "/root/item[contains(@name_es,'$term') or contains(php:functionString('strtolower',@name_es),'$term')]";
  6. } else {
  7. // fallback without PHP
  8. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.