zerGinfested Posted March 2, 2010 Share Posted March 2, 2010 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? Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 2, 2010 Share Posted March 2, 2010 Try this: $body = preg_replace("/\r?\n/","<br>", $row[body]); Quote Link to comment Share on other sites More sharing options...
zerGinfested Posted March 2, 2010 Author Share Posted March 2, 2010 Try this: $body = preg_replace("/\r?\n/","<br>", $row[body]); still returns the \r\n's all the same this applies to both the one's directly following/preceding a word ( for example: Proceeds\r\nfrom ) and seperate line breaks. Quote Link to comment Share on other sites More sharing options...
zerGinfested Posted March 2, 2010 Author Share Posted March 2, 2010 Been researching this for nearly 2 hours now. Anyone have any suggestions? Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 2, 2010 Share Posted March 2, 2010 It worked for me. Can you output the text from the database to a text file and attach here? Quote Link to comment Share on other sites More sharing options...
zerGinfested Posted March 3, 2010 Author Share Posted March 3, 2010 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]? Quote Link to comment Share on other sites More sharing options...
LeadingWebDev Posted March 3, 2010 Share Posted March 3, 2010 need to use inside preg_replace $row['body'] ? try it. Quote Link to comment Share on other sites More sharing options...
zerGinfested Posted March 3, 2010 Author Share Posted March 3, 2010 need to use inside preg_replace $row['body'] ? try it. didn't change anything :'( Quote Link to comment Share on other sites More sharing options...
LeadingWebDev Posted March 3, 2010 Share Posted March 3, 2010 it works for me as well like that $body = preg_replace("/\r\n/","<br>", $row['body']); check the query output so. Quote Link to comment Share on other sites More sharing options...
zerGinfested Posted March 3, 2010 Author Share Posted March 3, 2010 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]? Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 3, 2010 Share Posted March 3, 2010 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. Quote Link to comment Share on other sites More sharing options...
zerGinfested Posted March 3, 2010 Author Share Posted March 3, 2010 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... Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 3, 2010 Share Posted March 3, 2010 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. Quote Link to comment Share on other sites More sharing options...
zerGinfested Posted March 3, 2010 Author Share Posted March 3, 2010 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 3, 2010 Share Posted March 3, 2010 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. Quote Link to comment 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.