Get content through the php class

Asked

Viewed 24 times

2

How to parse to get content from the php class ?

<p class="p2 p2-resultado-busca"><span>Conteúdo desejado 1</span><strong>Conteúdo Indesejado</strong></p>
<p class="p2 p2-resultado-busca"><span>Conteúdo desejado 2</span><strong>Conteúdo Indesejado</strong></p>
<p class="p2 p2-resultado-busca"><span>Conteúdo desejado 3</span><strong>Conteúdo Indesejado</strong></p>
  • It will always be that same pattern of tags?

  • yes, yes, if you change with logic I adapt

1 answer

3


Using the class DOMDocument together with DOMXPath you can do it this way:

$html = <<<HTML
<p class="p2 p2-resultado-busca"><span>Conteúdo desejado 1</span><strong>Conteúdo Indesejado</strong></p>
<p class="p2 p2-resultado-busca"><span>Conteúdo desejado 2</span><strong>Conteúdo Indesejado</strong></p>
<p class="p2 p2-resultado-busca"><span>Conteúdo desejado 3</span><strong>Conteúdo Indesejado</strong></p>
HTML;

$dom = new domDocument('1.0', 'utf-8'); 
$dom->loadHTML($html); 
$dom->preserveWhiteSpace = false; 

$xpath = new DOMXPath($dom);
$content = $xpath->query('//p[@class="p2 p2-resultado-busca"]//span');

foreach($content as $span){
    echo $span->textContent;
}

See working on ideone

  • top was just, just added a echo utf8_decode($span->textContent); vlw a força man

Browser other questions tagged

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