Jump to content

preg_replace() & str_replace()


TechnoDiver

Recommended Posts

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?

Link to comment
Share on other sites

Out of curiosity, what do you see if you output them into textareas.

If my $row['content'] contains "abc\ndef\nghi", I see ...

image.png.e0e4b9011243e7cada489e27108f22ae.png

if I use this code ...

    $content = str_replace(["\r", "\n"], "", $row['content']);
    echo "<textarea>{$row['content']}</textarea><br>";
    echo "<textarea>$content</textarea><br>";

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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()

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.