2011-05-21 17 views
5

Tengo una cadena php que contiene el siguiente HTML que estoy recuperando de una fuente RSS. Estoy usando pastel simple y no puedo encontrar ninguna otra forma de dividir estos dos conjuntos de datos de <description>. Si alguien sabe de una forma en pastel simple para seleccionar niños, sería genial.forma PHP de analizar cadena HTML

<div style="example"><div style="example"><img title="example" alt="example" src="example.jpg"/></div><div style="example">EXAMPLE TEXT</div></div> 

to: 

$image = '<img title="example" alt="example" src="example.jpg">'; 
$description = 'EXAMPLE TEXT'; 
+0

http://stackoverflow.com/questions/7124823/file-get-html-displays-fatal -error-call-to-undefined-function – merrais

Respuesta

8
$received_str = 'Your received html'; 

$html = str_get_html($received_str); 

//Image tag 
$img_tag = $html->find("img", 0)->outertext; 

//Example Text 
$example_text = $html->find('div[style=example]', 0)->last_child()->innertext; 

Ver aquí: http://simplehtmldom.sourceforge.net/manual.htm

2

Trate Simple HTML Dom Parser

// Create DOM from HTML string 
$html = str_get_html('Your HTML here'); 

// Find all images 
foreach($html->find('img') as $element) 
     echo $element->src . '<br>'; 

// Description 
$description = $html->find('div[style=example]'); 
+0

'div [style = example]' coincide con tres nodos. –

0
try using strip_tags: 

<?php 
    $html ='<div style="example"><div style="example"><img title="example" alt="example" src="example.jpg"/></div><div style="example">EXAMPLE TEXT</div></div>'; 
    $html = strip_tags($html,'<img>'); //$HTML == <img title="example" alt="example" src="example.jpg"> 
    ?> 
+0

[No del todo] (http://www.ideone.com/bpWIA), pero es un paso en la dirección correcta. –