How to get a searchable value in a string in PHP?

0

I have a string that contains html code:

$pagina = "<!DOCTYPE html>
<html  lang='pt-BR'>
<title>Pagina</title>
<meta charset='UTF-8'>
<div class='conteudo'>
    <div class='valor'>
    1000
    </div>
</div>
</html>
";

$valor = $pagina //pegar na variável $pagina o que esta entre  <div class='valor'> e </div>

Is there a function in PHP that can help me?

    
asked by anonymous 03.12.2018 / 03:19

1 answer

2

Yes, it is possible.

$pagina = "<!DOCTYPE html>
  <html  lang='pt-BR'>
   <title>Pagina</title>
    <meta charset='UTF-8'>
    <div class='conteudo'>
     <div class='valor'>
     1000
     </div>
    </div>
  </html>
";
$doc = new \DOMDocument();
$doc->loadHTML($pagina);

$elements = $doc->getElementById("valor");

echo $elements[0]->nodeValue;

For more information: link

    
03.12.2018 / 03:48