Foros del Web » Programando para Internet » PHP »

Sacar saltos de linea

Estas en el tema de Sacar saltos de linea en el foro de PHP en Foros del Web. Hola, Tengo un archivo que PHP que crea uno JS. El problema es cuando lo escribo no me escribe el JS si tiene saltos de ...
  #1 (permalink)  
Antiguo 17/10/2008, 20:06
Avatar de pato12  
Fecha de Ingreso: septiembre-2007
Ubicación: Salta
Mensajes: 1.620
Antigüedad: 16 años, 6 meses
Puntos: 101
Sacar saltos de linea

Hola,
Tengo un archivo que PHP que crea uno JS.
El problema es cuando lo escribo no me escribe el JS si tiene saltos de linea.
Intente sacarlos con:
Código PHP:
$text=str_replace("(\n)+","",$text); 
Código PHP:
$text=str_replace("\n","",$text); 
Código PHP:
$text=str_replace("
"
,"",$text); 
Pero no puedo.
Si tengo de texto:
Cita:
<br />
<br />
<br />
me lo deja igual, enves de:
Cita:
<br /><br /><br />
Gracias
Salu2
__________________
Half Music - www.halfmusic.com
  #2 (permalink)  
Antiguo 17/10/2008, 20:23
 
Fecha de Ingreso: diciembre-2004
Mensajes: 272
Antigüedad: 19 años, 4 meses
Puntos: 4
Respuesta: Sacar saltos de linea

y si le pones doble diagonal ??


Código PHP:
$text=str_replace("\\n","",$text); 

Ahi me dices como te fue con eso, al raton!
  #3 (permalink)  
Antiguo 17/10/2008, 21:04
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: Sacar saltos de linea

primera, no confundas cadenas simples con expresiones regulares...

ahora, si los siguiente no te funciona haces algo ademas... que provoca tu error...

Código PHP:
$text preg_replace('/[\r?\n]+/'''$text); 
suerte!!
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #4 (permalink)  
Antiguo 17/10/2008, 22:14
Avatar de pato12  
Fecha de Ingreso: septiembre-2007
Ubicación: Salta
Mensajes: 1.620
Antigüedad: 16 años, 6 meses
Puntos: 101
Respuesta: Sacar saltos de linea

Hola,
Ninguna de las respuestas me funcionarion.
Yo saco el texto de la BD:
Cita:
Simona
<br />
<br />you`re getting older <br />
<br />
your journey`s been <br />
<br />
etched on your skin <br />
<br />
<br />
<br />
simona <br />
<br />
wish i had know that <br />
<br />
what seemed so strong <br />
<br />
has been and gone <br />
<br />
<br />
<br />
(coro) <br />
<br />
i will call you up every saturday <br />
<br />
night and e both stayed out til <br />
<br />
the morning light and we sang: <br />
<br />
here we go again <br />
<br />
and thought time goes by <br />
<br />
i wil always be <br />
<br />
in a club with you <br />
<br />
in 1973 <br />
<br />
singin here we go again <br />
<br />
<br />
<br />
simona <br />
<br />
wish i was sober <br />
<br />
so i could see clearly now <br />
<br />
the rain has gone <br />
<br />
<br />
<br />
simona <br />
<br />
i guess its over <br />
<br />
my memory plays our tune <br />
<br />
the same old song <br />
<br />
<br />
<br />
(coro) <br />
<br />
i will call you up every saturday <br />
<br />
night and e both stayed out til <br />
<br />
the morning light and we sang: <br />
<br />
here we go again <br />
<br />
and thought time goes by <br />
<br />
i wil always be <br />
<br />
in a club with you <br />
<br />
in 1973 <br />
<br />
singin here we go again <br />
<br />
<br />
<br />
(coro) <br />
<br />
i will call you up every saturday <br />
<br />
night and e both stayed out til <br />
<br />
the morning light and we sang: <br />
<br />
here we go again <br />
<br />
and thought time goes by <br />
<br />
i wil always be <br />
<br />
in a club with you <br />
<br />
in 1973 <br />
<br />
singin here we go again <br />
<br />
<br />
<br />
(coro) <br />
<br />
i will call you up every saturday <br />
<br />
night and e both stayed out til <br />
<br />
the morning light and we sang: <br />
<br />
here we go again <br />
<br />
and thought time goes by <br />
<br />
i wil always be <br />
<br />
in a club with you <br />
<br />
in 1973 <br />
<br />
singin here we go again <br />
<br />
<br />
<br />
i wil always be <br />
<br />
in a club with you <br />
<br />
in 1973 <br />
<br />
singin here we go again <br />
<br />
Y quiero sacarle los saltos de linea (no el <br />).
Pero si ago haci, sin sacarlo de la bd me anda:
Código PHP:
$text="Simona
<br />
<br />you`re getting older <br />
<br />
your journey`s been <br />
<br />
etched on your skin <br />
<br />
<br />
<br />
simona <br />
<br />
wish i had know that <br />
<br />
what seemed so strong <br />
<br />
has been and gone <br />
<br />
<br />
<br />
(coro) <br />
<br />
i will call you up every saturday <br />
<br />
night and e both stayed out til <br />
<br />
the morning light and we sang: <br />
<br />
here we go again <br />
<br />
and thought time goes by <br />
<br />
i wil always be <br />
<br />
in a club with you <br />
<br />
in 1973 <br />
<br />
singin here we go again <br />
<br />
<br />
<br />
simona <br />
<br />
wish i was sober <br />
<br />
so i could see clearly now <br />
<br />
the rain has gone <br />
<br />
<br />
<br />
simona <br />
<br />
i guess its over <br />
<br />
my memory plays our tune <br />
<br />
the same old song <br />
<br />
<br />
<br />
(coro) <br />
<br />
i will call you up every saturday <br />
<br />
night and e both stayed out til <br />
<br />
the morning light and we sang: <br />
<br />
here we go again <br />
<br />
and thought time goes by <br />
<br />
i wil always be <br />
<br />
in a club with you <br />
<br />
in 1973 <br />
<br />
singin here we go again <br />
<br />
<br />
<br />
(coro) <br />
<br />
i will call you up every saturday <br />
<br />
night and e both stayed out til <br />
<br />
the morning light and we sang: <br />
<br />
here we go again <br />
<br />
and thought time goes by <br />
<br />
i wil always be <br />
<br />
in a club with you <br />
<br />
in 1973 <br />
<br />
singin here we go again <br />
<br />
<br />
<br />
(coro) <br />
<br />
i will call you up every saturday <br />
<br />
night and e both stayed out til <br />
<br />
the morning light and we sang: <br />
<br />
here we go again <br />
<br />
and thought time goes by <br />
<br />
i wil always be <br />
<br />
in a club with you <br />
<br />
in 1973 <br />
<br />
singin here we go again <br />
<br />
<br />
<br />
i wil always be <br />
<br />
in a club with you <br />
<br />
in 1973 <br />
<br />
singin here we go again <br />
<br />"
;
$text preg_replace('/[\r?\n]+/'''$text);  
echo 
$text
¿Porque?
Graicas
aslu2
__________________
Half Music - www.halfmusic.com
  #5 (permalink)  
Antiguo 17/10/2008, 23:44
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: Sacar saltos de linea

pues a mi si me sale.... ¿que será?
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #6 (permalink)  
Antiguo 18/10/2008, 14:57
Avatar de pato12  
Fecha de Ingreso: septiembre-2007
Ubicación: Salta
Mensajes: 1.620
Antigüedad: 16 años, 6 meses
Puntos: 101
Respuesta: Sacar saltos de linea

Hola,
Si lo ago con el codigo que di hace rato si me sale.
Pero si remplaso $text, por un texto que resien saca de la bd no me anda.
Graicas
Salu2
__________________
Half Music - www.halfmusic.com
  #7 (permalink)  
Antiguo 18/10/2008, 15:03
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: Sacar saltos de linea

y como insertaste ese texto a la BD?

algunas veces si lo copias y pegas en un formulario, sobre todo... si lo sacaste de Word u otro software.... introduce caracteres "fantasma" (por así decirlo)

te invito a que hagas diversas de/codificaciones con esta cadena, en utf8 por ejemplo... htmlentites, etc... el punto es descubrir que carácter "que parece" un salto de linea no lo es... (porque deduzco, ese es el problema)
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #8 (permalink)  
Antiguo 18/10/2008, 17:51
Avatar de pato12  
Fecha de Ingreso: septiembre-2007
Ubicación: Salta
Mensajes: 1.620
Antigüedad: 16 años, 6 meses
Puntos: 101
Respuesta: Sacar saltos de linea

Hola,
Haci los inserto:
Código PHP:
$cansion=nl2br(addslashes(htmlentities(strip_tags($_POST['half_cansion']))));
if(
$cansion==""){
echo 
"La canción esta vacía.";
}elseif(empty(
$_POST['half_titulo'])){
echo 
"Titulo vacío, es un campo obligatorio.";
}else{
$album=($_POST['half_albun']=="")? "Desconocido" :trim(addslashes(htmlentities(strip_tags(ucfirst(strtolower($_POST['half_albun']))))));
$artista=($_POST['half_artista']=="")? "Desconocido" :trim(addslashes(htmlentities(strip_tags(ucfirst(strtolower($_POST['half_artista']))))));
$campos=array('artista','cansion','titulo','votos','votos_s','albun','ip','leidas','letra','por');
$datos=array($artista,$cansion,trim(addslashes(htmlentities(strip_tags(ucfirst(strtolower($_POST['half_titulo'])))))),'0','0',$album,$_SERVER['REMOTE_ADDR'],'0',substr($_POST['half_titulo'],0,1),$_SESSION['login_half']);
$c=implode(',',$campos);
$d='\''.implode("','",$datos).'\'';
$sSQL=@sprintf("INSERT INTO %s (%s) VALUES (%s)","cansiones",$c,$d);
$query = @mysql_query($sSQL);

Gracias
Salu2
__________________
Half Music - www.halfmusic.com
  #9 (permalink)  
Antiguo 18/10/2008, 21:46
okram
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Sacar saltos de linea

Mmmm... espero no estar diciendo una burrada

Y si usas mejor:
Código php:
Ver original
  1. $texto = preg_replace("/(\r\n)+|(\n|\r)+/", "", $texto);
?

Así cubres una posibilidad más, el \r solito. Podría ser, aunque sería muy extraño. Qué sistema operativo usas?

Saludos,
  #10 (permalink)  
Antiguo 18/10/2008, 21:56
Avatar de pato12  
Fecha de Ingreso: septiembre-2007
Ubicación: Salta
Mensajes: 1.620
Antigüedad: 16 años, 6 meses
Puntos: 101
Respuesta: Sacar saltos de linea

Hola okram,
Tu codigo me ando de 10.
Muchisimas graicas
Salu2
__________________
Half Music - www.halfmusic.com
  #11 (permalink)  
Antiguo 18/10/2008, 21:59
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: Sacar saltos de linea

okram, como que esta raro no?

según debería ser casi lo mismo con esta expresión..

Cita:
[\r?\n]+
¿o no?
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #12 (permalink)  
Antiguo 18/10/2008, 22:19
okram
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Sacar saltos de linea

No. Con esa expresión sólo consideras dos posibilidades: \r\n y \n. El signo de interrogación ? después de \r lo convierte en un caracter opcional, mas \n en esa expresión es obligatorio. Osea, estás descartando \r sólo. Con la expresión que yo dí se toma en cuenta las 3 posibilidades, y pues era justo lo que pasaba. Tenía los saltos de línea guardados como \r.

Y justo por eso le preguntaba acerca de su SO y tipo de ordenador, pues, hasta donde sé, el salto de línea \r es exclusivo de las Mac.

Saludos,
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:33.