Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Simple str_replace problem.

    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 )

    PHP Code:
    $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:
    PHP Code:
    $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

  • #2
    New Coder
    Join Date
    Jul 2009
    Posts
    91
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Silly question, have you got too many backslashes in there?
    $reply = str_replace("\r\n\\","<br>",$reply);
    ...
    $reply = str_replace("\r\n","<br>",$reply);

    ?

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    I think a better idea would be using the break parameter in wordwrap();

    PHP Code:
    wordwrap($text20"<br />\n"); 
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #5
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •