...

View Full Version : Simple str_replace problem.



jeddi
09-13-2009, 07:03 PM
Hi,
I am having trouble of a simple replacement of line feeds
in a variable that goes into an email.

I want to wordwrap to 70 characters and replace the line feeds with <br> so that it looks good in the email.

This is my 2 lines of code:
( Now I am sure that I used this before somewhere and it worked )


$reply = wordwrap($Db_rep,70);
$reply = nl2br($reply);


This produced this result:

Dear Dave,\r\n\r\nThanks for your support request message.
\r\n\r\nhope you got that cat that I sent\r\nyou in the
post.\r\n\r\nwas it still purring?

It is wordwrapped but the rn's are still there.

Then I tried:


$reply = wordwrap($Db_rep,70);
$reply = str_replace("\r\n\\","<br>",$reply);

Result:

Dear Dave,\r\n\r\nThanks for your support request message. \r\n\r\nhope you got that cat that I sent\r\nyou in the post.\r\n\r\nwas it still purring?

No line feed change and no wordwrap :(

Well I must be something wrong, but
I just can not see what it is :o

clunk.werclick
09-13-2009, 07:13 PM
Silly question, have you got too many backslashes in there?
$reply = str_replace("\r\n\\","<br>",$reply);
...
$reply = str_replace("\r\n","<br>",$reply);

?

Fou-Lu
09-13-2009, 10:14 PM
The wordwrap appears to work alright, but the nl2br is dying on you're \r\n.
nl2br is sensitive to both \n and \r\n, so the problem here is you're characters are literal: \\\r\\\n. The question is at which point is this happening.
Check you're database (I assumed db, since you're variable is $DB_rep) using either commandline or phpmyadmin to see if it shows as a newline in the text. If it doesn't, than it was inserted with mysql_real_escape_string, but not with stripslashes in an environment that uses magic_quotes_gpc. If it shows fine, its because you're magic_quotes_runtime is enabled, causing the escaping to happen when queried. Fix by setting set_magic_quotes_runtime(0);.
I find that its more likely from a magic_quotes_gpc environment, which means you'll need to manually edit these (or query to replace them, but there is no automatic fix for it).

Zangeel
09-13-2009, 10:50 PM
I think a better idea would be using the break parameter in wordwrap();


wordwrap($text, 20, "<br />\n");

jeddi
09-14-2009, 01:41 PM
Thanks for your replies,

I noticed that the problem was occurring when I sent a variable
that I had taken from a textarea in a form rather than from the data table.

The datbase retrieved data is fine, so I have solved the problem by reversing the flow. I now save the data and then retriev it before sending the email instead of sending the email and then saving the data.

It works fine now :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum