Ver Mensaje Individual
  #3 (permalink)  
Antiguo 13/12/2011, 19:51
Avatar de SirDuque
SirDuque
 
Fecha de Ingreso: febrero-2009
Ubicación: Paso del Rey, Buenos Aires, Argentina
Mensajes: 975
Antigüedad: 15 años, 2 meses
Puntos: 89
Respuesta: cortar un texto al encontrar determinado simbolo

Cita:
Iniciado por intiweb Ver Mensaje
Saludos, en una web que acabo de armar hice un pequeña función, no es para nada gran cosa pero me fue muy útil a lo largo de la aplicación para procesar los datos.
Asi que me pareció que podía compartirla con ustedes.


Código PHP:
function cortarString($texto,$simbolo){
    
/* SE LE PASAN 2 PARAMETROS EL PRIMERO ES EL TEXTO QUE SE QUIERE DIVIDIR Y EN EL SEGUNDO PARAMETRO SE PASA EL SIMBOLO QUE QUEREMOS USAR DE REFERENCIA PARA CORTAR EL TEXTO.
LA FUNCION AGARRA EL TEXTO ORIGINAL Y LO DIVIDE TOMANDO COMO LUGARES DE DIVISION EL SIMBOLO PASADO EN EL SEGUNDO PARAMENTRO Y TE DEVUELVE UN ARRAY CON LOS TEXTOS CORTADOS*/
    
// inicializo una variable en 0
// y la variable donde voy a ir guardando el string
    
$i 0;
    
$e 0;
    
$string "";
    
$array;
    
$length strlen($texto);
    
// con un loop while voy a ir recuperando el texto
    
while( $i $length){
        
        if ((
$texto[$i] != $simbolo) && ($i $length ))
        {
            
$string .= $texto[$i];    
        }
        else if (
$texto[$i] == $simbolo)
        {        
            
$array[$e] = $string;
            
$string "";
            
$e++;
        }
        else if(
$i == ($length 1))
        {    
        
            
$string .= $texto[$i];    
            
$array[$e] = $string;
            
$string "";
            
$e++;
        }
        
$i++;
    
    }
    
return 
$array;

saludos
Código PHP:
Ver original 
__________________
Mono programando!
twitter.com/eguimariano