Reputation: 485
I have a string from a textarea and I want to delete all new lines after the last word or character?
For example I have this string from the textbox
$str = "Hi, my name is Blah
x)
";
I need to remove new lines after the last word or character which is x)
So the results should be like this
$result = "Hi my name is Blah
x)";
I have tried str_replace, explode, rtrim but I cannot think of the algorithm.
Note: I don't want to remove ALL new lines. Just the one after the last word or character. TIA
Upvotes: 1
Views: 908
Reputation: 92785
What's wrong with rtrim()
? If you omit the second parameter it will do exactly what you want.
This function returns a string with whitespace stripped from the end of str.
Without the second parameter, rtrim() will strip these characters:
" " (ASCII 32 (0x20)), an ordinary space.
"\t" (ASCII 9 (0x09)), a tab.
"\n" (ASCII 10 (0x0A)), a new line (line feed).
"\r" (ASCII 13 (0x0D)), a carriage return.
"\0" (ASCII 0 (0x00)), the NULL-byte.
"\x0B" (ASCII 11 (0x0B)), a vertical tab.
Here is Codepad demo
Upvotes: 2