Con esta función Php podrás obtener un array que contenga todos los contenidos de un atributo determinado de un tag html determinado.
Por ejemplo podremos obtener todos los enlaces de imágenes de un código html, o el nombre de todas las clases usadas en un documento html.
Argumentos:
- $html: Variable que contiene la ruta del archivo html a analizar.
- $tag: Nombre del tag html que queremos analizar.
- $atribute: Atributo del tag html del cual que remos recolectar el contenido.
Función:
function get_html_atributes($html,$tag,$atribute){ if(!empty($html) && !empty($tag) && !empty($atribute)){ $html = file_get_contents($html); $doc = new DOMDocument(); @$doc->loadHTML($html); $array_final = array(); $imagenes = $doc->getElementsByTagName($tag); foreach ($imagenes as $imagen) { $array_final[] = $imagen->getAttribute($atribute); } return $array_final; } }
Ejecutar
$array = get_html_atributes('https://wordcodepress.com/','img','src');
Ejemplo
Array ( [0] => http://dantecreations.com/wp-content/uploads/2016/09/auto.png [1] => http://dantecreations.com/wp-content/uploads/2016/12/lega-e1482435411773-600x399.jpg [2] => http://dantecreations.com/wp-content/uploads/2016/12/MiniDeskTop-600x468.jpg [3] => http://dantecreations.com/wp-content/uploads/2016/11/MiniDeskTopq-600x412.jpg [4] => http://dantecreations.com/wp-content/uploads/2016/09/1463693488_573e30b0b9918-600x382.jpg [5] => http://dantecreations.com/wp-content/uploads/2016/09/2-1-600x367.jpg [6] => http://dantecreations.com/wp-content/uploads/2016/09/MiniLapTop-1-600x360.jpg [7] => http://dantecreations.com/wp-content/uploads/2016/09/1-2-600x360.jpg [8] => http://dantecreations.com/wp-content/uploads/2016/09/MiniLapTop-600x360.jpg [9] => http://dantecreations.com/wp-content/uploads/2016/09/3-1-600x507.jpg [10] => http://dantecreations.com/wp-content/uploads/2016/09/5-1-600x475.jpg [11] => http://dantecreations.com/wp-content/uploads/2016/09/6-1-600x450.jpg [12] => http://dantecreations.com/wp-content/uploads/2017/02/ALAMBRA2-150x150.jpg [13] => http://dantecreations.com/wp-content/uploads/2017/01/MiniPad_Black-150x150.jpg [14] => http://dantecreations.com/wp-content/uploads/2016/12/brunching-1-150x150.jpg [15] => http://dantecreations.com/wp-content/uploads/2016/12/neohome-150x150.jpg )
Si te ha sido de utilidad ¡deja un comentario!