<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Register PHP functions as XPath functions</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="domxpath.registernamespace.html">DOMXPath::registerNamespace</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.dom.html">DOM Functions</a></div> <div class="up"><a href="class.domxpath.html">DOMXPath</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="domxpath.registerphpfunctions" class="refentry"> <div class="refnamediv"> <h1 class="refname">DOMXPath::registerPhpFunctions</h1> <p class="verinfo">(PHP 5 >= 5.3.0)</p><p class="refpurpose"><span class="refname">DOMXPath::registerPhpFunctions</span> — <span class="dc-title">Register PHP functions as XPath functions</span></p> </div> <div class="refsect1 description" id="refsect1-domxpath.registerphpfunctions-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><span class="type void">void</span></span> <span class="methodname"><strong>DOMXPath::registerPhpFunctions</strong></span> ([ <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$restrict</code></span> ] )</div> <p class="para rdfs-comment"> This method enables the ability to use PHP functions within XPath expressions. </p> </div> <div class="refsect1 parameters" id="refsect1-domxpath.registerphpfunctions-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">restrict</code></em></span> <dd> <p class="para"> Use this parameter to only allow certain functions to be called from XPath. </p> <p class="para"> This parameter can be either a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> (a function name) or an <span class="type"><a href="language.types.array.html" class="type array">array</a></span> of function names. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-domxpath.registerphpfunctions-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> No value is returned. </p> </div> <div class="refsect1 examples" id="refsect1-domxpath.registerphpfunctions-examples"> <h3 class="title">Examples</h3> <p class="para"> The following examples use <var class="filename">book.xml</var> which contains the following: </p> <p class="para"> <div class="example" id="example-5435"> <p><strong>Example #1 book.xml</strong></p> <div class="example-contents"> <div class="xmlcode"><pre class="xmlcode"><?xml version="1.0" encoding="UTF-8"?> <books> <book> <title>PHP Basics</title> <author>Jim Smith</author> <author>Jane Smith</author> </book> <book> <title>PHP Secrets</title> <author>Jenny Smythe</author> </book> <book> <title>XML basics</title> <author>Joe Black</author> </book> </books></pre> </div> </div> </div> </p> <p class="para"> <div class="example" id="example-5436"> <p><strong>Example #2 <span class="methodname"><strong>DOMXPath::registerPHPFunctions()</strong></span> with <em>php:functionString</em></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$doc </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-></span><span style="color: #0000BB">load</span><span style="color: #007700">(</span><span style="color: #DD0000">'book.xml'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$xpath </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMXPath</span><span style="color: #007700">(</span><span style="color: #0000BB">$doc</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Register the php: namespace (required)<br /></span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-></span><span style="color: #0000BB">registerNamespace</span><span style="color: #007700">(</span><span style="color: #DD0000">"php"</span><span style="color: #007700">, </span><span style="color: #DD0000">"http://php.net/xpath"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Register PHP functions (no restrictions)<br /></span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-></span><span style="color: #0000BB">registerPHPFunctions</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Call substr function on the book title<br /></span><span style="color: #0000BB">$nodes </span><span style="color: #007700">= </span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">'//book[php:functionString("substr", title, 0, 3) = "PHP"]'</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Found </span><span style="color: #007700">{</span><span style="color: #0000BB">$nodes</span><span style="color: #007700">-></span><span style="color: #0000BB">length</span><span style="color: #007700">}</span><span style="color: #DD0000"> books starting with 'PHP':\n"</span><span style="color: #007700">;<br />foreach (</span><span style="color: #0000BB">$nodes </span><span style="color: #007700">as </span><span style="color: #0000BB">$node</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$title </span><span style="color: #007700">= </span><span style="color: #0000BB">$node</span><span style="color: #007700">-></span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"title"</span><span style="color: #007700">)-></span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)-></span><span style="color: #0000BB">nodeValue</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$author </span><span style="color: #007700">= </span><span style="color: #0000BB">$node</span><span style="color: #007700">-></span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"author"</span><span style="color: #007700">)-></span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)-></span><span style="color: #0000BB">nodeValue</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$title</span><span style="color: #DD0000"> by </span><span style="color: #0000BB">$author</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Found 2 books starting with 'PHP': PHP Basics by Jim Smith PHP Secrets by Jenny Smythe </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5437"> <p><strong>Example #3 <span class="methodname"><strong>DOMXPath::registerPHPFunctions()</strong></span> with <em>php:function</em></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$doc </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-></span><span style="color: #0000BB">load</span><span style="color: #007700">(</span><span style="color: #DD0000">'book.xml'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$xpath </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMXPath</span><span style="color: #007700">(</span><span style="color: #0000BB">$doc</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Register the php: namespace (required)<br /></span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-></span><span style="color: #0000BB">registerNamespace</span><span style="color: #007700">(</span><span style="color: #DD0000">"php"</span><span style="color: #007700">, </span><span style="color: #DD0000">"http://php.net/xpath"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Register PHP functions (has_multiple only)<br /></span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-></span><span style="color: #0000BB">registerPHPFunctions</span><span style="color: #007700">(</span><span style="color: #DD0000">"has_multiple"</span><span style="color: #007700">);<br /> <br />function </span><span style="color: #0000BB">has_multiple</span><span style="color: #007700">(</span><span style="color: #0000BB">$nodes</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// Return true if more than one author<br /> </span><span style="color: #007700">return </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$nodes</span><span style="color: #007700">) > </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />}<br /></span><span style="color: #FF8000">// Filter books with multiple authors<br /></span><span style="color: #0000BB">$books </span><span style="color: #007700">= </span><span style="color: #0000BB">$xpath</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">'//book[php:function("has_multiple", author)]'</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Books with multiple authors:\n"</span><span style="color: #007700">;<br />foreach (</span><span style="color: #0000BB">$books </span><span style="color: #007700">as </span><span style="color: #0000BB">$book</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$book</span><span style="color: #007700">-></span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"title"</span><span style="color: #007700">)-></span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)-></span><span style="color: #0000BB">nodeValue </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Books with multiple authors: PHP Basics </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-domxpath.registerphpfunctions-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="methodname"><a href="domxpath.registernamespace.html" class="methodname" rel="rdfs-seeAlso">DOMXPath::registerNamespace()</a> - Registers the namespace with the DOMXPath object</span></li> <li class="member"> <span class="methodname"><a href="domxpath.query.html" class="methodname" rel="rdfs-seeAlso">DOMXPath::query()</a> - Evaluates the given XPath expression</span></li> <li class="member"> <span class="methodname"><a href="domxpath.evaluate.html" class="methodname" rel="rdfs-seeAlso">DOMXPath::evaluate()</a> - Evaluates the given XPath expression and returns a typed result if possible</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="domxpath.registernamespace.html">DOMXPath::registerNamespace</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.dom.html">DOM Functions</a></div> <div class="up"><a href="class.domxpath.html">DOMXPath</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>