Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all newline sequences with commas [duplicate]

I'm using str_replace and it's not working correctly. I have a text area, which input is sent with a form. When the data is received by the server, I want to change the new lines to ",".

$teams = $_GET["teams"];
$teams = str_replace("\n",",",$teams);
echo $teams;

Strangely, I receive the following result

Chelsea

,real


,Barcelona

instead of Chealsea,real,Barcelona.

What's wrong?

like image 715
Omar Abid Avatar asked Nov 26 '25 16:11

Omar Abid


1 Answers

To expand on Waage's response, you could use an array to replace both sets of characters

$teams = str_replace(array("\r\n", "\n"),",",$teams);
echo $teams;

This should handle both items properly, as a single \n is valid and would not get caught if you were just replacing \r\n

like image 93
Jim Avatar answered Nov 29 '25 05:11

Jim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!