Selectind DOM elements based on attribute content

Posted by sameold on Stack Overflow See other posts from Stack Overflow or by sameold
Published on 2012-11-28T22:58:02Z Indexed on 2012/11/28 23:03 UTC
Read the original article Hit count: 216

Filed under:
|
|
|

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');

© Stack Overflow or respective owner

Related posts about php

Related posts about dom