TechnoDiver Posted August 15, 2021 Share Posted August 15, 2021 Can someone please tell me why neither one of these are working: $content = str_replace(array("\r", "\n"), '', $row['content']); $content = preg_replace( "/\r|\n/", '', $row['content']); I've tried each one of the to replace $content = $row['content']; and for some ungodly reason neither one of them are working. This is simply pulling content from a database but it keeps displaying \r\n when displayed in a page. Why? Quote Link to comment https://forums.phpfreaks.com/topic/313552-preg_replace-str_replace/ Share on other sites More sharing options...
requinix Posted August 15, 2021 Share Posted August 15, 2021 You mean you literally see a backslash and "r" or "n"? Quote Link to comment https://forums.phpfreaks.com/topic/313552-preg_replace-str_replace/#findComment-1589124 Share on other sites More sharing options...
Barand Posted August 15, 2021 Share Posted August 15, 2021 Out of curiosity, what do you see if you output them into textareas. If my $row['content'] contains "abc\ndef\nghi", I see ... if I use this code ... $content = str_replace(["\r", "\n"], "", $row['content']); echo "<textarea>{$row['content']}</textarea><br>"; echo "<textarea>$content</textarea><br>"; Quote Link to comment https://forums.phpfreaks.com/topic/313552-preg_replace-str_replace/#findComment-1589125 Share on other sites More sharing options...
TechnoDiver Posted August 15, 2021 Author Share Posted August 15, 2021 9 hours ago, requinix said: You mean you literally see a backslash and "r" or "n"? yea, and I can't get rid of them. It's strange nothing is working 7 hours ago, Barand said: Out of curiosity, what do you see if you output them into textareas. in textareas I still get the \r\n and also all the break tags as well (<br />). This uses a function to pull the posts from the database and then echos an html string with the properties inserted. It works fine if I just $content = $row['content']; except it's riddled with /r/n. Neither one of the ways I've used (str_replace & preg_replace) works. I'm stumped, I feel this is exactly the type of thing these functions were made for and can't imagine why they wouldn't work. Quote Link to comment https://forums.phpfreaks.com/topic/313552-preg_replace-str_replace/#findComment-1589132 Share on other sites More sharing options...
Barand Posted August 15, 2021 Share Posted August 15, 2021 30 minutes ago, TechnoDiver said: in textareas I still get the \r\n and also all the break tags as well (<br />). Break tags!. That's why I asked you to use a textarea. Break tags are causing the problem. You remove the newline characters but the break tags will make it look as though they are still there when you output to the browser. Don't store break tags in your data, just the newline characters. If you want line breaks in the browser output, add the break tags when you output to the browser with nl2br() Quote Link to comment https://forums.phpfreaks.com/topic/313552-preg_replace-str_replace/#findComment-1589135 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.