Re: explode string at new line

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jim Lucas wrote:
Chris wrote:
Davi wrote:
Em Terça 05 Junho 2007 23:52, heavyccasey@xxxxxxxxx escreveu:
That's exactly correct. Except I /think/ you should use "\n" instead of
'\n'.


Thank you for the reply... =)

I'll check this... BTW:

array explode ( string $delimiter, string $string [, int $limit] )

So, I was wrong...
The right way, probaly, is:

$str=explode("\n",$_POST["my_text"]);

If it's coming from a <textarea> you'll want to use "\r\n" because a textarea uses both a carriage return (\r) and newline (\n) to separate.

Otherwise each element of the array will have a \r on the end which may end up causing you some issues later on.

I was wondering about this, I seem to recall that window/mac/*nix all do it differently.

With a little Google'ing I came across this.

http://www.sitepoint.com/forums/printthread.php?t=54074

This was written in 2002, so I am not sure about the Mac = "\r".
Since they are using *nix now, it could be "\n" not sure

But the article had this to say:

Line breaks
People want to know how they can retain textarea line breaks in HTML. You should store text in the database in its original format (e.g. with just newlines) and then use nl2br() to convert newlines to HTML <br /> tags on display (thanks to the people here for teaching me that :)). That's all good, except for one problem with nl2br(): it doesn't seem to convert \r newlines (edit: this has now been fixed in PHP 4.2.0).

Windows uses \r\n newlines; *nix uses \n; Mac uses \r.

nl2br() works correctly on text from Windows/*nix because they contain \n. However, if you get text from a Mac, nl2br() will not convert its newlines (again, fixed in PHP 4.2.0). To remedy this, I use the following bit of code to convert \r\n or \r to \n before inserting it into the database. It won't hurt anything and ensures that nl2br() will work on the \n only newlines on display. Also, it has the side effect of saving 1 byte in the database per newline from Windows (by storing only \n instead of \r\n). :)

PHP Code:
$txt = preg_replace('/\r\n|\r/', "\n", $txt);

Fair enough :)

I guess my thinking was since a textarea is a html 'standard' it would be consistent across all browsers/systems. I guess not :)

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux