Buscar tag html o atributo de un tag html función php

En: Libre

12 ene 2010

WP Greet Box icon
Hola! Si estás nuevo por aquí y te gusta lo que ves, puedes disfrutar de una actualización automática y gratuita por RSS para leer nuestros artículos completos cuando lo deseas en tu lector RSS favorito.

Para realizar un analisis de un código html es posible que necesitemos consultar el contenido de un tag a través de su id o también obtener el valor de un atributo.
Por ejemplo:

  • Obtener el contenido 8 Días / 7 Noches de
    <span id="txt_dias"> 8   Días / 7   Noches</span>
    
  • Obtener el atributo class de
    <div id="id_div" class="formato1">Contenido a mostrar</div>
    
  • Obtener el atributo href de
    <a id="id_a" href="http://www.google.com.ec">Ir a Google</a>

Entonces podemos utilizar las siguientes funciones:


La función getContentById donde:

  • $id_tag Es el id del tag del que se obtendrá el atributo
  • $htmlEs el html que se analizará
  • $tags Con valor
    • 0 NO mostrará tags interiores al id solicitado
    • 1 mostrará tags interiores al id solicitado
< ?php
function getContentById($id_tag = "",$html="",$tags = 0)
{
	if ($id_tag == "" || $html == "")
		return "";
	if(strpos($html,$id_tag) === false)
		return "";
	if(getAtributeById($id_tag,"id",$html) != $id_tag)
		return "";
	$temp = explode($id_tag,$html);
	if(is_array($temp))
	{
		$temp_tag = substr($temp[0],strrpos($temp[0],'<'));
		$exp_tag = explode(' ',$temp_tag);
		$name_tag = str_replace("<","",$exp_tag[0]);
	}
	$close_tag = '";
	$pos_to_close = strpos($temp[1],'>');
	$content = substr($temp[1],$pos_to_close + 1,strpos($temp[1],$close_tag) - $pos_to_close - 1);
	$content = str_replace(array('á','é','í','ó','ú','Á','É','Í','Ó','Ú'),array('á','é','í','ó','ú','Á','É','Í','Ó','Ú'),$content);
	if(!$tags)
		return trim(strip_tags($content));
	else
		return trim($content);
}
?>

Y la funcion getAtributeById donde:

  • $id_tag Es el id del tag del que se obtendrá el atributo
  • $atribute Es el atribute que se desea obtener
  • $htmlEs el html que se analizará
< ?php
function getAtributeById($id_tag = "",$atribute = "",$html="")
{
	if ($id_tag == "" || $html == "")
		return "";
	if(strpos($html,$id_tag) === false)
		return "";
	$temp = explode($id_tag,$html);
	if(is_array($temp))
	{
		$temp_tag = substr($temp[0],strrpos($temp[0],'< '));
		$exp_tag = explode(' ',$temp_tag);
		$name_tag = str_replace("<","",$exp_tag[0]);
	}
	$temp = explode("<".$name_tag,$html);
	foreach($temp as $key => $t)
	{
		if(strpos($t,$id_tag) !== false)
		{
			$value_tag = trim($t);
			break;
		}
	}
	$only_atributes = substr($value_tag,0,strpos($t,">") - 1);
	$exp_atributes = explode("=",str_replace('=','*=',$only_atributes));
	$values_array = array();
	for($i = 0; $i < count($exp_atributes);$i++)
	{
		if($i == 0)
			$values_array[] = substr($exp_atributes[$i],0,strpos($exp_atributes[$i],'*'));
		else
		{
			$txt = trim($exp_atributes[$i]);
			$comilla = $txt[0];
			$value_comilla = substr($txt,1,strrpos($txt,$comilla)-1);
			$values_array[] = $value_comilla;
			if(strpos($txt,'*'))
				$values_array[] = trim(str_replace('*',"",substr($txt,strrpos($txt,$comilla)+1)));
		}
	}
	$atributes = array();
	for($i = 0; $i < count($values_array);$i++)
	{
		if($atribute != "" && $values_array[$i] == $atribute)
			return $values_array[$i+1];
		$atributes[$values_array[$i]] = $values_array[$i+1];
		$i++;
	}
	return $atributes;
}
?>

Otros post

Este post ha sido visto 837 veces

Deja tu comentario!

Acerca de UnderMedia

Inspiración y Determinación |
En Blog UnderMedia se recopilan y publican periódicamente noticias, artículos o comentarios generados dentro de UnderMedia S.A.

Twitter UnderMedia

  • Gaby: No le entendi XD [...]
  • makinista: Como podría hacer para que al hacer clic en un botón me cargara un html en otra parte de una web c [...]
  • Pablo: hola, muy bueno hombre, queria hacerte una pregunta, tal ves sea tonta no se, cual es la ventaja de [...]
  • Antony: Para ponerlo en tus páginas es necesario seguir estas instrucciones http://www.gentlesource.com/do [...]
  • Antony: Funciona perfecto. A los que les sale la página en blanco, si instalaron el modulo de lengua en esp [...]

Comunidad

Ultimas visitas

Raking

  • Blogalaxia
  • Top Blogs Ecuador

Switch to our mobile site