How to get a searched value in a PHP string?

Asked

Viewed 35 times

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 any PHP function that can help me?

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: http://php.net/manual/en/domdocument.getelementbyid.php

  • Thank you very much friend helped too much, I searched and had not found at all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.