Splitting Javascript and Joining PHP
PHP had functions split
and join
, but they are deprecated as aliases for the more widely used functions explode
and implode
. final initial version
What you are trying to do can be accomplished with implode
, explode
and str_replace
. The latter should replace any characters \r
(since they are optional). Then you can explode
use the string \n
as delimiter and implode
use again <br>
. But that would mean calling 3 functions, which is a bit overkill considering that there is only one function that does exactly what you want:nl2br
return implode(
'<br/>',
//split on \n
explode(
"\n",
//remove any \r chars
str_replace(
"\r",
'',
$s
)
)
);
//The results are the same as this clean, simple one-liner
return nl2br($s);
T; tr
use it nl2br
, it is that simple and it will do exactly what you want with a minimum of effort.
source to share