0

There is a user textarea input. I need to remove all new lines before saving (so it should be only one line).

I use

$text = str_replace("\r\n", "", $text);
$text = str_replace("\n", "", $text);

for this.

But one user entered text, that has new line, and it cannot be removed with that code.
I also tried \r\n and \r with no result.

I cannot copy it here, because as soon as I copy it, new line will be replaced with standard \n.

How can I remove this new line? Or how can I see what its character is?
It is saved in MySQL now.

Qiao
  • 16,565
  • 29
  • 90
  • 117
  • possible duplicate of [Reliably Remove Newlines From String](http://stackoverflow.com/questions/2274506/reliably-remove-newlines-from-string) – Gordon Jul 09 '11 at 08:03
  • No, it is not duplicate. I did not find answer there. – Qiao Jul 09 '11 at 08:19
  • ok, if that's not a duplicate, then how about http://stackoverflow.com/questions/5449580/replacing-r-n-with-php or http://stackoverflow.com/questions/3654844/php-str-replace-not-working-correctly or http://stackoverflow.com/questions/3779464/php-simple-way-to-replace-or-remove-empty-lines-with-str-replace or http://stackoverflow.com/questions/5057405/newline-question. And if you still insist that it's not a duplicate you should update your question to point out why it's not a duplicate of any of those because I have a hard time seeing any difference between your question and those. – Gordon Jul 09 '11 at 08:29

1 Answers1

2

Have you tried:

After clarification

// Order of replacement
$str     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order   = array("\r\n", "\n", "\r");
$replace = '';

// Processes \r\n's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $str);

This is direct from the PHP manual: http://php.net/manual/en/function.str-replace.php

sdolgy
  • 6,963
  • 3
  • 41
  • 61
  • Code from manual also does not work. Is is the same as mine, just using arrays. New line is still here. All others new lines works ok. – Qiao Jul 09 '11 at 08:11
  • how are you validating that the "new line is still here" ? – sdolgy Jul 09 '11 at 08:13
  • I see it here when it is loaded in the textarea. Also can see it in phpMyAdmin. There are all inputs from one user. May be he has some special OS. – Qiao Jul 09 '11 at 08:17
  • If you are unable to replicate the problem yourself ... by using a string with `\r\n` etc. i'd be inclined to think your code works and it's something else... maybe ^M ;) – sdolgy Jul 09 '11 at 08:19