Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Recentemente postei aqui no fórum para saber como pegar um valor do HTML remoto ex:
<ul>
<li>Valor 1</li>
<li>Valor 2</li>
</ul>
Obter o "Valor 1" e colocar em uma string. Até ai tudo bem, mais depois começou a aparecer um problema, se eu quiser-se pegar um texto pequeno não dava muito certo. Por Ex.:
Uma página qualquer tem esse texto:
<div id="dados">
<p><em>Lorem Ipsum</em> is simply dummy text of the printing and typesetting industry. <br />
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. </p>
<ul class="test">
<li>Valor 1</li>
<li>Valor 2</li>
</ul>
<p>It has survived not only five centuries, <strong>but also the leap into electronic typesetting</strong>, remaining essentially unchanged. </p>
<p>It was popularised in the <em>1960s with the release</em> of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p>
</div>
Quero pega-la com os p, ul e as outras formatações, mais não da certo, fica assim na string
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
Valor 1
Valor 2
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Como posso fazer para também pegar as formatações, parágrafos, quebras de linhas, ...?
o Código que eu estou utilizando é esse:
<?php
function pegarHTML ($pg,$expressao,$item){
if ($item == "") { $item = 0;}
$dom = new DOMDocument;
@$dom->loadHTML( file_get_contents( $pg ) );
$xpath = new DOMXPath( $dom );
$conteudo = $xpath->query( "$expressao" )->item( $item )->nodeValue;
return $conteudo;
}
$pagina = 'http://ehaqui.com/gerador/test1.php';
$expressao1 = "//div[@id='dados']";
$conteudo1 = pegarHTML ($pagina,$expressao1,"");
echo $conteudo1;
?>
Podem me dar uma ajuda com um exemplo, pois ainda sou novo no PHP e não sei muita coisa :D
Carregando comentários...