Ene 12
Escrito por: Daniel Ardila
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 = ''.$name_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 260 veces

Últimos comentarios