I have several types of <a> in my document. I’d like to select only the <a> whose title attribute begins with the text “more data …” like <a title="more data *">
For example given the markup below, I’d like to select the first and second <a>, but skip the third because title doesn’t begin with more data, and skip the 4th because <a> doesn’t even have a title attribute.
<a title="more data some text" href="http://mypage.com/page.html">More</a>
<a title="more data other text" href="http://mypage.com/page.html">More</a>
<a title="not needed" href="http://mypage.com/page.html">Not needed</a>
<a href="http://mypage.com/page.html">Not needed</a>
I’m using DOMXPath. How would my query look like?
$xpath = new DOMXPath($doc);
$q = $xpath->query('//a');
You could use a query like:
Which uses a predicate such that the
titleattribute’s value must start with the specified string.An example
And here’s the above example running online.