Foros del Web » Programando para Internet » PHP »

Problema con funcion para Validar existencia de atributos en un html con DOMDocument

Estas en el tema de Problema con funcion para Validar existencia de atributos en un html con DOMDocument en el foro de PHP en Foros del Web. Saludos Compañeros !!! Estoy desarrollando un validador personalizado de tag y atributos html. Leyendo las reglas por medio de un xml pero me esta dando ...
  #1 (permalink)  
Antiguo 08/12/2011, 08:53
 
Fecha de Ingreso: octubre-2009
Mensajes: 19
Antigüedad: 14 años, 6 meses
Puntos: 0
Busqueda Problema con funcion para Validar existencia de atributos en un html con DOMDocument

Saludos Compañeros !!!

Estoy desarrollando un validador personalizado de tag y atributos html. Leyendo las reglas por medio de un xml pero me esta dando un error:

Fatal error: Can't use function return value in write context in func.php on line 15

Y no lo he podido solucionar alguien tiene alguna idea de como solucionarlo o alguna luz para poder solucionarlo.

Aqui les dejo los codigos de los files que utilizo:

Archivo Php parser.php
Código PHP:
<?
include_once('inc/func.php');
$htmlUrl "data/email.html"
$html file_get_contents($htmlUrl);
$xmlUrl "data/rules.xml"
$xmlStr file_get_contents($xmlUrl);
$xmlObj simplexml_load_string($xmlStr);
foreach (
$xmlObj->rule as $rule)
{
    if (
$rule['category']=='tag')
    {
        if (
tagExitsInTag($rule->tagname,$html) == $rule['valid'])
        {
         echo(
"Uso de la Etiqueta ".$rule->tagname." Es invalido");
        }
    }
}
?>
Archivo XML : rules.xml
Código HTML:
<?xml version="1.0" encoding="utf-8"?>
<rules>
  <rule category="tag" valid="FALSE">
   <tagname>p</tagname>
   <msg>invalid tag</msg>
  </rule>
    <rule category="attrib" valid="FALSE">
   <tagname>a</tagname>
   <attibute>title</attibute>
   <msg>invalid attribute</msg>
  </rule>
</rules> 
Archivo PHP: func.php
Código PHP:
<?php
//funcion para saber si existe un atributo dentro de un tag
function attrbibuteExitsInTag($eti,$attribute,$html)
{
    @
$dom = new DOMDocument();
    @
$dom->loadHTML($html);
    
$xpath = new DOMXPath($dom);
    
$tags $xpath->evaluate("/html/body//".$eti);
    for (
$i 0$i $tags->length$i++)
    {
        
$tag $tags->item($i);
        
$r=$tag->hasAttribute($attribute);
        if (
$r== TRUE)
        {
            
$items($i)= FALSE;
        }
        else
        {
            
$items($i)=TRUE;
        }
    }
    return 
$items;
}
//funcion para  saber si existe un Tag de terminado
function tagExitsInTag($eti,$html)
{
    @
$dom = new DOMDocument();
    @
$dom->loadHTML($html);
    
$xpath = new DOMXPath($dom);
    
$tags $xpath->evaluate("/html/body//".$eti);
    
$tags->length;
    
$tag $tags->item(0);
    
$v=empty($tag);
     if (
$v==TRUE)
     {
      return 
true;
     }
    else
     {
      return 
FALSE;
     }
}
?>
Gracias pro la aydua de antemano :D
  #2 (permalink)  
Antiguo 08/12/2011, 09:19
 
Fecha de Ingreso: septiembre-2007
Ubicación: PyRoot
Mensajes: 1.515
Antigüedad: 16 años, 6 meses
Puntos: 188
Respuesta: Problema con funcion para Validar existencia de atributos en un html con D

Hola bueno para empezar creo que esto no está bien:

Código PHP:
Ver original
  1. $v=empty($tag);

En PHP existe el operador ternario

Código PHP:
Ver original
  1. $v= (empty($tag)) ? true : false;

y luego la condicional yo lo haría así:

Código PHP:
Ver original
  1. if($v === true) return true;
  2. if( $v === false ) return false;

Bueno en realidad yo lo dejaría así:

Código PHP:
Ver original
  1. if ($v===TRUE) return true;
  2.  
  3. return false;
__________________
Si quieres agradecer el triangulo obscuro de la parte derecha debes presionar +.
  #3 (permalink)  
Antiguo 09/12/2011, 12:01
 
Fecha de Ingreso: octubre-2009
Mensajes: 19
Antigüedad: 14 años, 6 meses
Puntos: 0
Respuesta: Problema con funcion para Validar existencia de atributos en un html con D

Gracias iovan pero el problema estaba en la forma que estructure el array use ( en vez de [ Por aquello aqui les dejo el codigo corregido:
Archivo XML rules.xml
Código HTML:
<?xml version="1.0" encoding="utf-8"?>
<rules>
  <rule category="tag" valid="FALSE">
   <tagname>p</tagname>
   <msg>Uso ino debido de la etiqueta P</msg>
  </rule>
    <rule category="attrib" valid="FALSE">
   <tagname>a</tagname>
   <attibute>title</attibute>
   <msg>Falta atributo title en las etiquetas a</msg>
  </rule>
</rules> 
Archivo PHp parser.php
Código PHP:
<?
include_once('inc/func.php');
$htmlUrl "data/email.html"
$html file_get_contents($htmlUrl);
$xmlUrl "data/rules.xml"
$xmlStr file_get_contents($xmlUrl);
$xmlObj simplexml_load_string($xmlStr);
foreach (
$xmlObj->rule as $rule)
{
    if (
$rule['category']=='tag')
    {
        if (
tagExitsInTag($rule->tagname,$html) == $rule['valid'])
        {
         echo(
"Uso de la Etiqueta ".$rule->tagname." ".$rule->msg."<br/>");
        }
    }
    else
    {
        if (
$rule['category']=='attrib')
        {
            
$val=attrbibuteExitsInTag($rule->tagname,$rule->attibute,$html);
            for (
$i 0$i count($val); $i++)
            {        
                if (
$val[$i] == $rule['valid'])
                {
                  echo(
"Uso de la Etiqueta ".$rule->tagname."  ".$rule->msg."<br/>");
                }
            
            }    
        }
    }
}
?>
Archivo PHP func.php
Código PHP:
<?php
//funcion para  saber si existe un atributo dentro de un tag
function attrbibuteExitsInTag($eti,$attribute,$html)
{
    @
$dom = new DOMDocument();
    @
$dom->loadHTML($html);
    
$xpath = new DOMXPath($dom);
    
$tags $xpath->evaluate("/html/body//".$eti);
    for (
$i 0$i $tags->length$i++)
    {
        
$tag $tags->item($i);
        if (
$tag->hasAttribute($attribute))
        {
            
$items[$i]= FALSE;
        }
        else
        {
            
$items[$i]=TRUE;
        }
    }
    return(
$items);
}
//funcion para  saber si existe un Tag de terminado
function tagExitsInTag($eti,$html)
{
    @
$dom = new DOMDocument();
    @
$dom->loadHTML($html);
    
$xpath = new DOMXPath($dom);
    
$tags $xpath->evaluate("/html/body//".$eti);
     if ((
$tags->length) > 0)
     {
      return 
TRUE;
     }
    else
     {
      return 
FALSE;
     }
}
?>

Etiquetas: atributos, domdocument, existencia, funcion, html
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:59.