Since this is a piece of HTML code, you can use the class DOMDocument
:
$qtd = '<div id="itens">
<span>
435 itens encontrados
</span>
</div>';
$doc = new \DOMDocument();
$doc->loadHTML($qtd);
$elements = $doc->getElementsByTagName("div");
echo $elements[0]->nodeValue;
In this way, the result of the echo
would be the content of div
, including the tag span
, as described in the question. But if the desired content is span
, just change the name of the tag in:
$doc->getElementsByTagName("div");
// -------------------------^
As follows:
$qtd = '<div id="itens"><span>435 itens encontrados</span></div>';
$doc = new \DOMDocument();
$doc->loadHTML($qtd);
$elements = $doc->getElementsByTagName("span");
echo $elements[0]->nodeValue;
The result will be:
435 itens encontrados
Very good! I’ll remember this forever now, thank you!
– Charles Fay