-2

I am trying to remove these \n\n characters that appear in my string when I post a form.

image showing example of n\n\ problem

My code is;

$text = $textdb['columnname'];
echo trim(preg_replace('/\s\s+/', ' ', $text));

But it doesn't work. I have tried literally every example here and still nothing works. Am I suppose to do something prior to this? Does this work with a certain version of php? Am using 5.6 by the way. Thanks.

Jay Smoke
  • 539
  • 3
  • 13
  • 31
  • `nl2br()` is an option? – nice_dev May 05 '21 at 18:50
  • @nice_dev just tried it and it didn't work. I did $text = nl2br($textdb['columnname']); – Jay Smoke May 05 '21 at 19:03
  • First I would do a `var_dump($textdb['columnname']);` in order to see exactly what is in that db field. I suspect that `str_replace("\n", "", $text);` will work. – FoulFoot May 05 '21 at 19:11
  • @FoulFoot thanx. I did that and I got string(128) so it is a string. str_replace didn't work either :(( is there any possibility that this feature is disabled from the server? – Jay Smoke May 05 '21 at 19:19
  • They do not "appear" without you having put them there. How are you retrieving this data from the database? How are you storing it? Why are you using a version of PHP that's been obsolete for years? – miken32 May 05 '21 at 19:53
  • @miken32 am not sure which question you are answering. Can you explain the significance of these questions to the problem at hand?! – Jay Smoke May 05 '21 at 21:25
  • `var_dump` should show the actual string that's being stored. If you do view-source in your browser you'll see the actual string characters, including whether the newline "\n" is present, or it's some different character. `str_replace` wouldn't be disabled on the server, no. – FoulFoot May 05 '21 at 23:25
  • @FoulFoot yeah I just checked and it's there. string(128) "Oooooh shoot, Punchline!!!!\n\n\"and leaving the Lord/am like a journalist, am never giving up my source\" ‍♂️" that's what I got from the source – Jay Smoke May 06 '21 at 00:20
  • `\s` in your regular expression would match any whitespace character, including newlines. **But you do not have any actual newlines here.** You have the character backslash, followed by the character `n`, _in text form_. Learn to differentiate between _code_, and _data_. – CBroe May 06 '21 at 09:21
  • @CBroe how does your answer solve the problem? Can you explain please. Thanx – Jay Smoke May 06 '21 at 09:23
  • 1
    `str_replace("\n", "", $text);` would try to replace an actual newline. If you want to replace the characters backslash-followed-by-n instead, then you simply need to supply a text literal containing _those_ as the first parameter. `str_replace('\n', "", $text);` – CBroe May 06 '21 at 09:26
  • @CBroe it worked. But can you explain why it sees it as text and not a new line even though it was created as a new line? Thanks – Jay Smoke May 06 '21 at 09:36
  • Can’t tell you that, with the information we got so far - no one here knows what _“even though it was created as a new line”_ is actually supposed to mean. All we know right now, is what `$textdb['columnname']` contained. Could be that the data got messed up before you inserted it into the database, could be that it got messed up when you read it back. – CBroe May 06 '21 at 09:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232032/discussion-between-jay-smoke-and-cbroe). – Jay Smoke May 06 '21 at 10:02

1 Answers1

-1

Just use nl2br

Example:

$text = nl2br("hey\n neighbor");

would print

hey
neighbor
elfantoche
  • 116
  • 1
  • 8