I want to skip the first "a" <a href="URL#" title="TITLE">TITLE</a>
And select all others under class posted-in
$models = $xpath->query("//p[@class='posted-in']//a");
for ($i = 0; $i < $models->length; $i++) {
$result->add_model($models->item($i)->getAttribute('a'));
}
HTML:
<p class="posted-in">
Posted in <a href="URL#" title="TITLE">TITLE</a>
on Mar 16, 2017 featuring <a href="URL#">MODEL1</a>,
<a href="URL#">MODEL2</a>,
<a href="URL#">MODEL2</a></p>
I'm using this code with tag "alt" Goodwork
$models = $xpath->query("//li[@class='models']//img");
for ($i = 0; $i < $model->length; $i++) {
$result->add_model($models->item($i)->getAttribute('alt'));
}
But I'm Confused... How can I select the a
text?
The text content of an element node is available in the property DOMElement::$textContent
. This value includes any descendant text node.
If you need to ignore the first node in your result you can add a condition.
$html = <<<'HTML'
<p class="posted-in">
Posted in <a href="URL#1" title="TITLE">TITLE</a>
on Mar 16, 2017 featuring <a href="URL#2">MODEL1</a>,
<a href="URL#3">MODEL2</a>,
<a href="URL#4">MODEL2</a></p>
HTML;
$document = new DOMDocument();
$document->loadHtml($html);
$xpath = new DOMXpath($document);
$expression = '//p[@class="posted-in"]/a[position() > 1]';
foreach ($xpath->evaluate($expression) as $a) {
var_dump($a->textContent, $a->getAttribute('href'));
}
Output:
string(6) "MODEL1"
string(5) "URL#2"
string(6) "MODEL2"
string(5) "URL#3"
string(6) "MODEL2"
string(5) "URL#4"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.