Jump to content

Recommended Posts

if(isset($id)){
	$query = "SELECT * FROM news where id='$id'"; 
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result) or die(mysql_error());
	$body = preg_replace("'\r?\n'","<br>", $row[body]); 
	print 
	"<div class='articletitle'>$row[title]</div><br />
        <div class='maintext'><em>Released $row[date]</em><br><br>$body</div>"
	;}

 

The above code is NOT stripping my text of \r\n\, nor of \n. Anyone have any idea as to why not?

Link to comment
https://forums.phpfreaks.com/topic/193923-preg_replace-rn/
Share on other sites

It worked for me. Can you output the text from the database to a text file and attach here?

 

it does work for me, but only when i define the variable in the code beforehand (i.e. plain text, not from the db). do I need to use return () or something like that so it actually reads the variable $row[body]?

Link to comment
https://forums.phpfreaks.com/topic/193923-preg_replace-rn/#findComment-1020703
Share on other sites

it works for me as well like that

$body = preg_replace("/\r\n/","<br>", $row['body']); 

check the query output so.

 

it works fine when i copy the text and put it into a variable

 

i.e. when i go $body = "blahblahblahblahblahblah" it will remove the r\n\ correctly

 

but when i try to preg_replace in the actual variable ($row[body]) it doesn't do it. is this because php doesnt read the actual variable, and just the characters of $row[body]?

Link to comment
https://forums.phpfreaks.com/topic/193923-preg_replace-rn/#findComment-1020965
Share on other sites

is this because php doesnt read the actual variable, and just the characters of $row[body]?

 

That statement doesn't make sense. PHP reads the "value" of the variable. So, the value you are getting from the database must not contain the characters you think it does. I would suggest running a preg_replace to just remove any \r's and then use nl2br() on the result. See what that does.

Link to comment
https://forums.phpfreaks.com/topic/193923-preg_replace-rn/#findComment-1020970
Share on other sites

is this because php doesnt read the actual variable, and just the characters of $row[body]?

 

That statement doesn't make sense. PHP reads the "value" of the variable. So, the value you are getting from the database must not contain the characters you think it does. I would suggest running a preg_replace to just remove any \r's and then use nl2br() on the result. See what that does.

 

it does contain the characters I think it does - here's a snippit from the start of the value:

 

<p align=\"center\"><strong>TITLE HERE</strong></p>\r\n              <p>Vancouver,\r\nBC:&

 

which is being output (whether I use preg_replace, str_replace, whatever) as

 

TITLE HERE

\r\n

 

Vancouver,\r\nBC

 

I thought I read somewhere that nl2br() is outdated now? and I don't want to replace the \r\n's with line breaks or <br>'s, just filter them out completely as the structure if wrapped in <p> tags...

Link to comment
https://forums.phpfreaks.com/topic/193923-preg_replace-rn/#findComment-1020974
Share on other sites

Hold on, there's some conflicting information here. In your original post you showed this code

$body = preg_replace("'\r?\n'","<br>", $row[body]);

 

But, now you are saying you don't want to replace the line breaks with BR tags?!

 

Also, it appears you are saying the "original" content contains this:

  
<p align=\"center\"><strong>TITLE HERE</strong></p>\r\n              <p>Vancouver,\r\nBC:&
  

and the output looks like this:

TITLE HERE
\r\n

Vancouver,\r\nBC

 

Is that correct? How, exactly, is the original content defined? It looks like you are saving the ACTUAL characters \r and \n and NOT the escape codes for line breaks. You would never "see" the \r\n codes. Something is fishy here. If you are seeing the actual characters \r\n, then this should remove them

$body = preg_replace("/[\\\][r][\\\][n]/", '', $row['body']);

 

But, a bigger question is why those characters are in your data at all. There seems to be a problem with how the data is defined/saved.

Link to comment
https://forums.phpfreaks.com/topic/193923-preg_replace-rn/#findComment-1020981
Share on other sites

I realised that the <p> tags would suffice in structuring the layout of the posts, and that the <br> would just create empty spaces that wouldn't look right.

 

Is that correct? How, exactly, is the original content defined? It looks like you are saving the ACTUAL characters \r and \n and NOT the escape codes for line breaks. You would never "see" the \r\n codes. Something is fishy here. If you are seeing the actual characters \r\n, then this should remove them
$body = preg_replace("/[\\\][r][\\\][n]/", '', $row['body']);

 

I'm using the Wyzz WYSIWYG editor for the text field, and copying and pasting the bodies of each post from the existing web page to the mysql db -- the existing web page being entirely html. The \r\n must have been actual characters and not codes, because that preg_replace worked. What do the square brackets define in this case?

 

I definitely have to tell you how appreciative I am of your help on this. I haven't taken any schooling for php yet, so my methods of learning are pretty much to code until I encounter a problem, then scour google until I find a solution. Sadly NO ONE has addressed this issue before, is it not that common of a problem? (in this case resulting due to my improper coding?). Again, thank you.

Link to comment
https://forums.phpfreaks.com/topic/193923-preg_replace-rn/#findComment-1020989
Share on other sites

The problem has been addressed previously. I found the solution on a different forum post here: http://www.webdeveloper.com/forum/showthread.php?t=195186

 

The brackets are needed because \r and \n are interpreted as newline/linbreak characters. So, you have to use the brackets and extra slashes to make the regular expression interpreter know you are wanting the actual characters and not the newline characters. I'm actually surprised that three backslashes are required - I would have expected two were needed.

Link to comment
https://forums.phpfreaks.com/topic/193923-preg_replace-rn/#findComment-1021030
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.