Ver Mensaje Individual
  #2 (permalink)  
Antiguo 24/09/2010, 13:19
Avatar de repara2
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