Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>What am i doing wrong?</p> </blockquote> <p>The xpath expression <code>@id</code> (second parameter) returns an array but <code>preg_match</code> expects a string.</p> <p>Convert it to string first: <code>string(@id)</code>.</p> <p>Next to that you need to actually compare the output to 1 as <code>preg_match</code> returns <code>1</code> when found:</p> <pre><code>foreach($xpath-&gt;query("//table[@id and 1 = php:function('preg_match', '/post\d+/', string(@id))]") as $key =&gt; $row) { var_dump($key, $row, $row-&gt;ownerDocument-&gt;saveXml($row)); } </code></pre> <p><strong>Explanation/What happens here?:</strong></p> <p>A xpath expression will by default return a node-list (more precisely node-set). If you map a PHP function onto such expressions these sets are represented in form of an array. You can easily tests that by using <code>var_dump</code>:</p> <pre><code>$xpath-&gt;query("php:function('var_dump', //table)"); array(1) { [0]=&gt; object(DOMElement)#3 (0) { } } </code></pre> <p>Same for the xpath expression <code>@id</code> in the context of each table element:</p> <pre><code>$xpath-&gt;query("//table[php:function('var_dump', @id)]"); array(1) { [0]=&gt; object(DOMAttr)#3 (0) { } } </code></pre> <p>You can change that into a string typed result by making use of the <a href="http://www.w3.org/TR/xpath/#section-String-Functions" rel="nofollow">xpath <code>string</code> function</a>:</p> <blockquote> <p>A node-set is converted to a string by returning the string-value of the node in the node-set that is first in document order. If the node-set is empty, an empty string is returned.</p> </blockquote> <pre><code>$xpath-&gt;query("//table[php:function('var_dump', string(@id))]"); string(4) "test" </code></pre> <p>(the table has <code>id="test"</code>)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload