Código de PHP - parsetHTML - analiza una cadena HTML y extrae los tags con su atributos

Versión 1

Publicado el 30 de Septiembre del 2013gráfica de visualizaciones de la versión: Versión 1
4.078 visualizaciones desde el 30 de Septiembre del 2013
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

Puedes ver un ejemplo aquí
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/*
 * parseHtml.php
 * Author: Carlos Costa Jordao
 * Email: carlosjordao@yahoo.com
 *
 * Notacion de las variables:
 * i_ = integer, ex: i_count
 * a_ = array, a_html
 * b_ = boolean,
 * s_ = string
 *
 * Que hace esta función:
 * - analiza una cadena HTML y extrae los tags con su atributos
 * - exceptions: los tags html como <br> <hr> </a>, etc
 * - Al finalizar, devuelve un array de este tipo:
 * ["IMG"][0]["SRC"] = "xxx"
 * ["IMG"][1]["SRC"] = "xxx"
 * ["IMG"][1]["ALT"] = "xxx"
 * ["A"][0]["HREF"] = "xxx"
 *
 */
function parseHtml( $s_str )
{
	$i_indicatorL = 0;
	$i_indicatorR = 0;
	$s_tagOption = "";
	$i_arrayCounter = 0;
	$a_html = array();
	// Busca un cualquier tab (<) dentro de la cadena
	while(is_int(($i_indicatorL=strpos($s_str,"<",$i_indicatorR))))
	{
		// Get everything into tag...
		$i_indicatorL++;
		$i_indicatorR = strpos($s_str,">", $i_indicatorL);
		$s_temp = substr($s_str, $i_indicatorL, ($i_indicatorR-$i_indicatorL) );
		$a_tag = explode( ' ', $s_temp );
		// Here we get the tag's name
		list( ,$s_tagName,, ) = each($a_tag);
		$s_tagName = strtoupper($s_tagName);
 
		// Well, I am not interesting in <br>, </font> or anything else like that...
		// So, this is false for tags without options.
		$b_boolOptions = is_array(($s_tagOption=each($a_tag))) && $s_tagOption[1];
		if( $b_boolOptions )
		{
			// Without this, we will mess up the array
			$i_arrayCounter = (int)count($a_html[$s_tagName]);
			// get the tag options, like src="htt://". Here, s_tagTokOption is 'src' and s_tagTokValue is '"http://"'
 
			do {
				$s_tagTokOption = strtoupper(strtok($s_tagOption[1], "="));
				$s_tagTokValue = trim(strtok("="));
				$a_html[$s_tagName][$i_arrayCounter][$s_tagTokOption] = $s_tagTokValue;
				$b_boolOptions = is_array(($s_tagOption=each($a_tag))) && $s_tagOption[1];
			} while( $b_boolOptions );
		}
	}
	return $a_html;
}
 
print_r(parseHtml("<p><div><a href='http://www.yahoo.es'>Yahoo</a></div></p><img src='file1.jpg'><img src='file2.jpg'>"));
?>



Comentarios sobre la versión: Versión 1 (0)


No hay comentarios
 

Comentar la versión: Versión 1

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s2497