How to remove "\\r\\n"?

Discussion in 'PHP' started by fixesign, Mar 28, 2011.

  1. #1
    Friends, if I edit my articles, always appear like this "\\r\\n". How to remove it? I use "stripslashes" but does not function properly. Help me, friends.
     
    fixesign, Mar 28, 2011 IP
  2. TimK

    TimK Peon

    Messages:
    51
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $newString = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "", $string);
    PHP:
     
    TimK, Mar 28, 2011 IP
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3
    Emm using @TimK's code, I think this might be easier, plus you'll what the returns and next line codes replaced with <br /> so try this instead.

    $newString = preg_replace("#\\\\r\\\\n#", "<br />", $string);
    PHP:
     
    MyVodaFone, Mar 28, 2011 IP
  4. ajaxboys

    ajaxboys Greenhorn

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    I think stripslashes() is the right way to avoid this. if you want my help put your code here, i will try once.
     
    ajaxboys, Mar 28, 2011 IP
  5. fixesign

    fixesign Member

    Messages:
    175
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #5
    How to insert the code above?? Sorry friends, I still do not understand.
    To save the articles to the database, I have a code like this:
    function antislashes($data){
      $anti = stripslashes($data);
      return $anti;
    }
      $content = antislashes($_POST[text_area]);
    PHP:
    Thanks advance, friends.
     
    fixesign, Mar 29, 2011 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    Is this something you added because of the \\r\\n problem ?
    function antislashes($data){
      $anti = stripslashes($data);
      return $anti;
    }
      $content = antislashes($_POST[text_area]);
    PHP:
    What is the normal method for displaying the content/articles, is echo $data or $content ?
     
    MyVodaFone, Mar 29, 2011 IP
  7. fixesign

    fixesign Member

    Messages:
    175
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #7
    \\r\\n suddenly appears when I edit articles, and in the database \\r\\n stored as text.

    ex:
    The\\r\\n visitor/candidate registration enables attending visitors and \\r\\ncandidates to register their career, employment, education, and training\\r\\n aspirations. They only have to log in to the link, NCEE 2011 Visitor /\\r\\n Candidate Registration, and complete the registration form.

    This is my code:
    function antislashes($data){
      $anti = stripslashes($data);
      return $anti;
    }
      $content = antislashes($_POST[text_area]);
    
    mysql_query("UPDATE article SET tab_article = '$content' WHERE id_article = '$_POST[id]'");
    PHP:
    Method for displaying the content / articles is:
    $article = nl2br ($a[tab_article]); echo "$article";
    PHP:
    Do I have to replace as below, to remove \\r\\n and it will never comes back:
    function antislashes($data){
      $anti = preg_replace("#\\\\r\\\\n#", "", $data);
      return $anti;
    }
      $content = antislashes($_POST[text_area]);
    mysql_query("UPDATE article SET tab_article = '$content' WHERE id_article = '$_POST[id]'");
    PHP:
     
    Last edited: Mar 29, 2011
    fixesign, Mar 29, 2011 IP
  8. goldseiker

    goldseiker Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thansk for your help :D
     
    goldseiker, Mar 29, 2011 IP
  9. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #9
    Yes, you replaced that correctly fixesign.

    However, I strongly recommend you to escape all data you're using in your query, as this is a huge security risk your taking. I'd suggest you escape the ID and content.
    Please refer to this page for more information: http://php.net/manual/en/function.mysql-real-escape-string.php
     
    Sky AK47, Mar 29, 2011 IP
  10. fixesign

    fixesign Member

    Messages:
    175
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #10
    When I tried the offline mode, it works very well. But when online, the code above is really not able to function so that \\r\\n is still stored in the database. Is something wrong? Help me, friends....
     
    fixesign, Mar 31, 2011 IP