outputting \r\n after text is decoded - PHP

半城伤御伤魂 提交于 2019-12-24 06:41:57

问题


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!


回答1:


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




回答2:


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().




回答3:


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



来源:https://stackoverflow.com/questions/10337206/outputting-r-n-after-text-is-decoded-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!