Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update: Xpath version of <code>*[@class~='my-class']</code> css selector</strong></p> <p>So after my comment below in response to hakre's comment, I got curious and looked into the code behind <code>Zend_Dom_Query</code>. It looks like the above selector is compiled to the following xpath (untested):</p> <p><code>[contains(concat(' ', normalize-space(@class), ' '), ' my-class ')]</code></p> <p>so the php would be:</p> <pre><code>$dom = new DomDocument(); $dom-&gt;load($filePath); $finder = new DomXPath($dom); $classname="my-class"; $nodes = $finder-&gt;query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]"); </code></pre> <p>Basically, all we do here is normalize the <code>class</code> attribute so that even a single class is bounded by spaces, and the complete class list is bounded in spaces. Then append the class we are searching for with a space. This way we are effectively looking for and find only instances of <code>my-class</code>.</p> <hr> <p>Use an xpath selector?</p> <pre><code>$dom = new DomDocument(); $dom-&gt;load($filePath); $finder = new DomXPath($dom); $classname="my-class"; $nodes = $finder-&gt;query("//*[contains(@class, '$classname')]"); </code></pre> <p>If it is only ever one type of element you can replace the <code>*</code> with the particular tagname.</p> <p>If you need to do alot of this with very complex selector i would recommend <a href="http://framework.zend.com/manual/en/zend.dom.query.html" rel="noreferrer"><code>Zend_Dom_Query</code></a> which supports CSS selector syntax (a la jQuery):</p> <pre><code>$finder = new Zend_Dom_Query($html); $classname = 'my-class'; $nodes = $finder-&gt;query("*[class~=\"$classname\"]"); </code></pre>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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