Línea nueva dentro de cadena variable

What if I have a long input of string? maybe 50-250 characters. and I echoed the variable it would echo a very long line of letters and words.

So, how will I let the variable auto Newline itself? maybe after 50 characters it would new line when echoed.

I have thought of counting the length of the string and looping it and after counting 50 characters it would Newline but if i do that it would slow down the Site.

is there any function to Newline a VARIABLE STRING? or i should use Looping instead?

preguntado el 10 de marzo de 12 a las 09:03

You don't need to insert newlines. If you're outputting into html it will automatically wrap within its containing element. If you're outputting into a JavaScript script block inside a string variable nobody will see the result anyway. (If you did want to do it anyway you don't necessarily need a loop, you could easily do it with a regex replace.) Note also that inserting a newline every x characters won't look right unless you use a monospace font. -

5 Respuestas

You can use a built-in PHP function: wordwrap.

If you want to cut at 50 characters and insert a HTML line-break: wordrap($str, 50, "<br />"); You can also use the last parameter to force return, even if the string does not contain spaces.

respondido 10 mar '12, 09:03

i tried wordwrap, but i can't make it work. is there something wrong with this line of codes? <?php $str = $row_Personalinfo['hospitalized']; $newstr = wordwrap($str,20,"<br />\n"); echo $newstr;?> - Christian Eric Paran

It only works when you have space on your string. it helped me thanks. - Christian Eric Paran

Use the fourth parameter to force line break. - Guillaume Poussel

You can insert escape characters into a string, such as \n which will be translated to a new line break.

respondido 10 mar '12, 09:03

Puede usar el PHP_EOL in conjunction with strlen() to acheive what you want.

respondido 10 mar '12, 09:03

Just adding more info about \n:

 // PHP 5.5.12

 // This works for outputing to browser:
 $XmlHeader = '<?mso-application progid="Excel.Sheet"?>' . "\n";

 // These do NOT work:
 $XmlHeader = '<?mso-application progid="Excel.Sheet"?>' . '\n';
 $XmlHeader = '<?mso-application progid="Excel.Sheet"?>\n';

Respondido el 30 de Septiembre de 15 a las 10:09

Esto no proporciona una respuesta a la pregunta. Para criticar o solicitar una aclaración de un autor, deje un comentario debajo de su publicación; siempre puede comentar sus propias publicaciones, y una vez que tenga suficiente reputación usted será capaz de comenta en cualquier publicación. - SuperBiasedMan

Correct this was not intended to answer the discriptive question. But if we just read the title of this question this answer might help someone in future. I only wanted to comment on other answer but couldn't because of lack of rep. - Zunair

If you can manage the string in php i think the definitive solution is using function wordwrap: http://www.php.net/manual/en/function.wordwrap.php

You can set the max lenght of a row, the braek parameter (in your case /n).

The function returns a string with the /n breaks and you can print it.

respondido 10 mar '12, 09:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.