Mariusz
Mariusz

Reputation: 65

Get something inside a tag with preg match all php

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?

Upvotes: 0

Views: 145

Answers (2)

trincot
trincot

Reputation: 350272

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

Upvotes: 1

Drew
Drew

Reputation: 4373

Assuming you want to get the text APRILIA Tuono V4 1100 Factory, 175 PS from your example string above then this expression will work

^[^>]+>([^<]+)

https://regex101.com/r/Xnt0Ls/1

Upvotes: 0

Related Questions