Replace \n in form submitted to DB

Discussion in 'PHP' started by lektrikpuke, Feb 13, 2009.

  1. #1
    Hi y'all,

    I have a php form that keeps track of my notes via MySQL. I decided to find a way to replace /n with <br /> and have tried the following 3 ways:

    $lng_desc=mysql_real_escape_string(nl2br($_POST['lng_desc']));
    $lng_desc=mysql_real_escape_string(str_replace("\n","<br />",($_POST['lng_desc'])));
    $lng_desc=mysql_real_escape_string(preg_replace('/\n/','<br />',($_POST['lng_desc'])));

    All three ways do an excellent job of inserting the breaks. My only problem is that if I modify the same field, the breaks are reinserted (so 2 breaks per \n). If I modify again, I get even more breaks (even though I'm not adding any \n's - I can just resubmit without changing a thing and more show up).

    So, this means the \n's are not really being replaced. The \n's are found and a break added next to it.

    What am I doing wrong?

    Thanks as always. =)

    Rob :D
     
    lektrikpuke, Feb 13, 2009 IP
  2. Shoro

    Shoro Peon

    Messages:
    143
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this.

    
    $replacearray = array("\r\n","\n\r","\n","\r");
    $lng_desc=mysql_real_escape_string(str_replace($replacearray,"<br />",($_POST['lng_desc'])));
    
    PHP:
     
    Shoro, Feb 13, 2009 IP
  3. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #3
    Thank you. That did it. Obviously, I wasn't replacing everything. Thanks again. :D
     
    lektrikpuke, Feb 13, 2009 IP