I have this HTML:
<a href="somelink;ref=start" class="head-title">APRILIA Tuono V4 1100 Factory, 175 PS</a>
It is possible to get the text between: class="head-title">
and </a>
in preg_match_all
?
I have this HTML:
<a href="somelink;ref=start" class="head-title">APRILIA Tuono V4 1100 Factory, 175 PS</a>
It is possible to get the text between: class="head-title">
and </a>
in preg_match_all
?
You should not use regular expressions for parsing HTML, as it will very likely break when your HTML input gets more complicated.
Use the DOM parser that PHP provides:
function getNodeText($html, $classname) {
$dom = new DomDocument();
$dom->loadHTML($html);
$finder = new DomXPath($dom);
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
return $nodes[0]->textContent;
}
$html = '<a href="somelink;ref=start" class="head-title">APRILIA Tuono V4 1100 Factory, 175 PS</a>';
$classname="head-title";
echo getNodeText($html, $classname); // APRILIA Tuono V4 1100 Factory, 175 PS
Assuming you want to get the text APRILIA Tuono V4 1100 Factory, 175 PS
from your example string above then this expression will work
^[^>]+>([^<]+)