Reputation: 4695
Say I have a text area, user enters information exactly like styled below:
Ice cream
Chocolate
then submits this information, I want to retrieve the information EXACTLY like so:
Ice cream, Chocolate
Is this the best way to do it:
$arr = explode("\n", $var);
$arr = implode(",", $arr);
When doing it like this, it puts the information out like so:
Ice cream , Chocolate
Note the space after cream, will a simple trim()
fix this?
Upvotes: 3
Views: 1806
Reputation: 2724
Dont store modified data in db, just store original one so when you present it to user they can edit them easily.
As far as displaying them, when you present output to users' browser use regex as mentioned by zrekms above
$text = preg_replace("~\s*[\r\n]+~", ', ', $text);
Above line is good enough.
Oh BTW for those who want to explode new line but dont know how because they are different on each OS, Instead of doing this...
$arr = explode("\n", $var);
$arr = explode("\r\n", $var);
$arr = explode("\r", $var);
Just use
$arr = explode(PHP_EOL, $var);
PHP_EOL is equivalent to new line char and its OS independent.
Upvotes: 1
Reputation: 20431
You could use preg_split with regular expressions:
http://php.net/manual/en/function.preg-split.php
Upvotes: -2
Reputation: 12749
Just a guess, but will "\r\n" in the explode correct the issue? I know in Windows Based machines a new line is just that.
Upvotes: 0