转换< br />到\ n再回来(Textarea) [英] Converting <br/> to \n and Back Again (Textarea)

查看:66
本文介绍了转换< br />到\ n再回来(Textarea)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网站,我的客户端将能够使用输入框维护网站上的一些文本,并由PHP和MySQL驱动。我遇到的问题是让他们很容易将文本输入到textarea并让它自动格式化< br />返回笔划的标签(textarea中的\ n)。我知道如何将\ n'转换为"< br />"当使用str_replace将新项目添加到数据库中时。但是,当他们稍后去编辑该字段时,我需要它将< br />返回到\ n'(在textarea中),以便返回原始格式他们输入并且他们不必担心任何代码。

解决方案

nl2br()

或者简单地

str_replace()


不那么简单。 < b取代;标签可以有3种不同的格式,因此您必须应对所有这些格式。您可以使用正则表达式,但以下内容将使其更清晰:

[php]


new_string = str_replace(array(" < br>","< br />","< br />"),array(" \ n"," \ n"," \ N'QUOT),

I''m working on a site where my client will be able to maintain some of the text on the site using input boxes and such driven by PHP and MySQL. The problem I am running into is making it easy for them to enter text into a textarea and have it automatically format the "<br/>" tags from their return strokes ("\n" in the textarea). I know how to convert the \n''s to "<br/>" when a new item is added into the database using "str_replace". However, when they go to edit that field later on, I need it to convert the "<br/>"''s back to \n''s (in a textarea) so it returns the original formatting they typed in and they don''t have to worry about any code.

解决方案

nl2br()
or, simply
str_replace()


Not that simple. <b> tags can have 3 different formats, so you have to cope with all of them. You could use a regular expression, but the following will make it more clear:

[php]


new_string=str_replace(array("<br>","<br/>","<br />"), array("\n","\n","\n"),


这篇关于转换&lt; br /&gt;到\ n再回来(Textarea)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆