سؤال

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!

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

I have checked your decoded string Hello\r\nWorld. in windows operating system using zend studio 7.2.0. I found nl2br() is working fine in both case \r\n and \n. You should double check your decoded string. May be you are doing addslashes() before encoding it.If so then you have to use stripslashes() before using nl2br().

You can use stripslashes($text) to strip those. However, I'd recommend finding out why the slashes are added. Are you using addslashes ?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top