Foros del Web » Programando para Internet » PHP »

buscar img sin alt

Estas en el tema de buscar img sin alt en el foro de PHP en Foros del Web. ayuda, nose como hacer uan funcion para buscar dentro de un archivo o un string muy grande. lo que quiero es buscar dentro de el ...
  #1 (permalink)  
Antiguo 23/09/2010, 11:19
 
Fecha de Ingreso: octubre-2009
Mensajes: 19
Antigüedad: 14 años, 6 meses
Puntos: 0
buscar img sin alt

ayuda, nose como hacer uan funcion para buscar dentro de un archivo o un string muy grande. lo que quiero es buscar dentro de el los tag img pero buscar todos lo que no tenga alt y despeus agregarle alt="". si tiene alguna idea o ejempl oque me sea util celos agradezco.
  #2 (permalink)  
Antiguo 24/09/2010, 13:19
Avatar de repara2  
Fecha de Ingreso: septiembre-2010
Ubicación: München
Mensajes: 2.445
Antigüedad: 13 años, 7 meses
Puntos: 331
Respuesta: buscar img sin alt

Aquí tienes el script, puede haber otras maneras de hacerlo pero me parece que todavía no tienes los conocimientos necesarios. Esta es una opción, el código es simple (fuera de la función). Modifica a gusto y espero que funcione:

Código PHP:
<?php

//la cadena o archivo que DEBES tener como una variable
$string "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />
<title>Documento sin t&iacute;tulo</title>
</head>

<body>
<table width=\"400\" border=\"0\">
  <tr>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_pab.gif\" width=\"259\" height=\"32\" alt=\"texto alternativo\" longdesc=\"texto alternativo\" /></td>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_pbb.gif\" width=\"259\" height=\"32\" alt=\"texto alternativo\" longdesc=\"texto alternativo\" /></td>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_pcb.gif\" width=\"259\" height=\"32\" alt=\"texto alternativo\" longdesc=\"texto alternativo\" /></td>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_pdb.gif\" width=\"259\" height=\"32\" longdesc=\"texto alternativo\" /></td>
  </tr>
  <tr>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_peb.gif\" width=\"259\" height=\"32\" alt=\"texto alternativo\" longdesc=\"texto alternativo\" /></td>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_pfb.gif\" width=\"259\" height=\"32\" alt=\"texto alternativo\" longdesc=\"texto alternativo\" /></td>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_pgb.gif\" width=\"259\" height=\"32\" alt=\"texto alternativo\" longdesc=\"texto alternativo\" /></td>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_phb.gif\" width=\"259\" height=\"32\" longdesc=\"texto alternativo\" /></td>
  </tr>
  <tr>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_pib.gif\" width=\"259\" height=\"32\" alt=\"texto alternativo\" longdesc=\"texto alternativo\" /></td>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_pjb.gif\" width=\"259\" height=\"32\" alt=\"texto alternativo\" longdesc=\"texto alternativo\" /></td>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_pkb.gif\" width=\"259\" height=\"32\" alt=\"texto alternativo\" longdesc=\"texto alternativo\" /></td>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_plb.gif\" width=\"259\" height=\"32\" longdesc=\"texto alternativo\" /></td>
  </tr>
  <tr>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_pmb.gif\" width=\"259\" height=\"32\" alt=\"texto alternativo\" longdesc=\"texto alternativo\" /></td>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_pnb.gif\" width=\"259\" height=\"32\" alt=\"texto alternativo\" longdesc=\"texto alternativo\" /></td>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_pob.gif\" width=\"259\" height=\"32\" alt=\"texto alternativo\" longdesc=\"texto alternativo\" /></td>
    <td><img src=\"file:///C|/Archivos de programa/Zend/Apache2/htdocs/apache_ppb.gif\" width=\"259\" height=\"32\" longdesc=\"texto alternativo\" /></td>
  </tr>
</table>
</body>
</html>"
;

echo 
"<h1>Antes</h1>".$string;
$imgs get2DelimitedString($string"<"">"); //Esta llamada no se puede cambiar, obtiene todas las string entre < y >

$lo_que_quiero_encontrar "alt=\"";
$lo_que_quiero_agregar " alt=\" el nuevo alt\"";
//Vamos a ver si tiene alt, si no lo tiene se le pone
    
$x 0;
    foreach(
$imgs as $tag){
        if(
strstr($tag$lo_que_quiero_encontrar)) continue; //Si tiene alt continuo a la siguiente
        //reemplazo en el texto original la etiqueta por la misma pero con alt
        
$string str_replace($tag$tag.$lo_que_quiero_agregar$string);
        
$x++;
        
    }
    
    echo 
"<h1>Realizados $x Reemplazos</h1>";
    echo 
"<h1>Despues</h1>".$string//Ahora string tiene al vuebo valor


/**

     * Obtiene la cadena entre dos delimitadores

     * cbruschi.blosgspot.com

     * @param <string> $xString

     * @param <string> $xDelimiter0

     * @param <string> $xDelimiter1

     * @return <array> con los string que se encuentran en delimitador0 y delimintador1

     */

    
function get2DelimitedString($xString$xDelimiter0$xDelimiter1){



        if(!
$xLength strlen($xDelimiter0))die("El parámetro 1 está vacío en get2DelimitedString()!");

        if(!
$xLength strlen($xDelimiter1))die("El parámetro 2 está vacío en get2DelimitedString()!");

        if(!
$xLength strlen($xString))die("La cadena está vacío en get2DelimitedString()!");



        
$xPosition = array();



        
//Obtener la posición del delimitador 0 y 1

        
for($i 0$i<$xLength$i++){

            if((
substr($xString$i1) == $xDelimiter0) || substr($xString$i1) == $xDelimiter1)

            
$xPosition[] = $i;

        }



        
//Cortar la cadena por la posición indicada

        
for($j=0;$j<count($xPosition);$j++){

            if(
substr(substr($xString, ($xPosition[$j]+1), (($xPosition[($j+1)]-$xPosition[$j])-1)), 03)=="img")
            
$xReturn[] = substr($xString, ($xPosition[$j]+1), (($xPosition[($j+1)]-$xPosition[$j])-1));

            
$j++;

        }



        return (
$xReturn);



    }

?>
Salu2
  #3 (permalink)  
Antiguo 24/09/2010, 14:56
 
Fecha de Ingreso: octubre-2009
Mensajes: 19
Antigüedad: 14 años, 6 meses
Puntos: 0
Respuesta: buscar img sin alt

Gracias, si tienes razon estoy muy noob con esto.

Etiquetas: img, altas
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 13:22.