PHP Simple HTML DOM Parser

Desde hace tiempo estaba buscando esta librería muy útil para parsear código HTML, pues bien ayer tuve la fortuna de encontrarla y hoy quiero compartirla con ustedes, esta librería se llama “PHP Simple HTML DOM Parser

// Extraer el código HTML
echo file_get_html(‘http://www.google.com/’)->plaintext;

// Obtener todos los enlaces
$ret = $html->find(‘a‘);

// todas las divisiones con el atributo id=’foo’
$ret = $html->find(‘div[id=foo]‘);

// Todas las divisiones con que contengan el atributo “id”
$ret = $html->find(‘div[id]‘);

// Elementos con “id=foo”
$ret = $html->find(‘#foo‘);

//Todos los elementos <li> dentro de <ul>
$es = $html->find(‘ul li‘);

// Todas las divisiones  anidadas hasta profundidad 3
$es = $html->find(‘div div div‘);

// Todos los<td> en<table>
$es = $html->find(‘table.hello td‘);

Estas son algunas funciones útiles al momento de parsear código HTML con PHP.

Te invito a darle una leida a la documentación online.

Sitio oficial. http://simplehtmldom.sourceforge.net/

Leave a Reply

Your email address will not be published. Required fields are marked *