phpencodingstr-replacenl2br

outputting \r\n after text is decoded - PHP


I am building an application where users can enter a note into a textarea. The note is then encoded when entered into the database. When I decode the returned value, the nl2br function isn't working. It isn't replacing the \r\n with the br tag. When not encoding/decoding text, it works.

I am using the following code to encode/decode my information: http://www.myphpscripts.net/tutorial.php?id=9

If I enter into a textarea:

Hello  
World

It encodes it, and then returns when decoded

Hello\r\nWorld.

I can do a str_replace, but as I come to understand, depending on the browser, a textarea may use \n or \r instead of \r\n.

Not sure what a good solution is... please help! Thank you!


Solution

  • If the text you are converting is in a "<textarea>" do not use "<br />".

    If you are converting text to be placed in a text area, use...

    str_ireplace("\r\n", "\n", $db_string); //or something similar
    

    If you are converting text to be placed OUTSIDE a text area, use...

    str_ireplace(array("\r\n", "\n", "\r"), '<br />', $db_string);
    

    Separate browsers use whatever, but I believe they all can read just "\n". However on a side note, the operating system determines "\r\n" vs "\n". Just use "\n". So replace "\r\n" with "\n". Or just leave "\r\n", use either, as long as you don't put br tags into your textarea tags you'll be all set.

    Mac = Linux = \n Windows = \r\n