Omar Abid Omar Abid - 1 year ago 115
PHP Question

PHP str_replace not working correctly

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




instead of Chealsea,real,Barcelona.

What's wrong?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download