diff --git a/reference/dom/domxpath/registerphpfunctions.xml b/reference/dom/domxpath/registerphpfunctions.xml new file mode 100644 index 0000000000..572f8043bd --- /dev/null +++ b/reference/dom/domxpath/registerphpfunctions.xml @@ -0,0 +1,194 @@ + + + + + + DOMXPath::registerPhpFunctions + Register PHP functions as XPath functions + + + + &reftitle.description; + + public voidDOMXPath::registerPhpFunctions + mixedrestrict + + + This method enables the ability to use PHP functions within XPath expressions. + + + + + + &reftitle.parameters; + + + + restrict + + + Use this parameter to only allow certain functions to be called from XPath. + + + This parameter can be either a string (a function name) or + an array of function names. + + + + + + + + + &reftitle.returnvalues; + + &return.void; + + + + + &reftitle.examples; + + The following examples use book.xml which contains the following: + + + + book.xml + + + + + PHP Basics + Jim Smith + Jane Smith + + + PHP Secrets + Jenny Smythe + + + XML basics + Joe Black + + +]]> + + + + + + <methodname>DOMXPath::registerPHPFunctions</methodname> with <literal>php:functionString</literal> + +load('book.xml'); + +$xpath = new DOMXPath($doc); + +// Register the php: namespace (required) +$xpath->registerNamespace("php", "http://php.net/xpath"); + +// Register PHP functions (no restrictions) +$xpath->registerPHPFunctions(); + +// Call substr function on the book title +$nodes = $xpath->query('//book[php:functionString("substr", title, 0, 3) = "PHP"]'); + +echo "Found {$nodes->length} books starting with 'PHP':\n"; +foreach ($nodes as $node) { + $title = $node->getElementsByTagName("title")->item(0)->nodeValue; + $author = $node->getElementsByTagName("author")->item(0)->nodeValue; + echo "$title by $author\n"; +} + +?> +]]> + + &example.outputs.similar; + + + + + + + + <methodname>DOMXPath::registerPHPFunctions</methodname> with <literal>php:function</literal> + +load('book.xml'); + +$xpath = new DOMXPath($doc); + +// Register the php: namespace (required) +$xpath->registerNamespace("php", "http://php.net/xpath"); + +// Register PHP functions (has_multiple only) +$xpath->registerPHPFunctions("has_multiple"); + +function has_multiple($nodes) { + // Return true if more than one author + return count($nodes) > 1; +} +// Filter books with multiple authors +$books = $xpath->query('//book[php:function("has_multiple", author)]'); + +echo "Books with multiple authors:\n"; +foreach ($books as $book) { + echo $book->getElementsByTagName("title")->item(0)->nodeValue . "\n"; +} + +?> +]]> + + &example.outputs.similar; + + + + + + + + + &reftitle.seealso; + + + DOMXPath::registerNamespace + DOMXPath::query + DOMXPath::evaluate + + + + + + +