如何更正HTML文本换行

时间:2015-01-28 05:47:19

标签: html coldfusion textwrapping

这是一个基本的HTML文本包装问题,但它让我难过。我在后端使用冷敷。我有以下模板:

 This line has 30 characters it
 This line has 40 characters it a b a b a
 This line has 50 characters it a b a b a as d f e
 This line has 60 characters it a b a b a as d f e we r t yy 
 This line has 70 characters it a b a b a as d f e we r t yy e 4 r t 5 

我将模板加载到textarea中,让用户编辑它以生成如下报告:

 <textarea name="Report" cols="72" rows="40">#template#</textarea>

然后我添加pre / pre以保留任何添加的标点符号:

 <CFSET #Report# = "<pre>" &   #Report#   & "</pre>" >

然后我将报告保存在sql db中作为文本变量。 但是当我尝试检索包含设置为72的报告和输出时,

 <CFSET #Report# = #wrap(#Report#, 72)#>

我得到了这个奇怪的间距,我无法弄清楚:

 This line has 30 characters it
 This line has 40 characters it a b
 a b a
 This line has 50 characters it a b a b a as d f e
 This line has
 60 characters it a b a b a as d f e we r t yy 
 This line has 70
 characters it a b a b a as d f e we r t yy e 4 r t 5 

我已经用这个破坏了我的大脑,非常感谢任何建议或意见。

1 个答案:

答案 0 :(得分:0)

我从来没有真正使用Wrap(),但看起来,在ACF中,它只是每72个字符插入一个中断(Railo似乎注意放置)。

您有几个选择。

最简单的解决方案是使用这样的替换来轻松播放数据。

#REReplace(variable,"(\r\n|\n\r|\r|\n)","<br>\1","ALL")#.

或者#ParagraphFormat(variable)#可能是你的盟友。

在这些情况下你不需要PRE包装器,尽管你可以使用css来设置等宽字体,如果这就是你使用pre的原因。由于它不在PRE中,因此您的内容将遵循其容器的参数(尽管您可能需要为容器设置宽度。

可能有效的其他东西,如果您决定保持效果是这样,但要注意它不会考虑标签。虽然设计确实考虑了查找空格或换行符,但如果您有一些文本如<input type=",并且输入恰好超过了阈值,那么您最终会在该空间中休息。

#rereplace(variable,"([^\n]{1,71})[\s]","\1<br>","ALL")#

<cfsavecontent variable="seventytwo">abcdefghijklmnopqrstabcdefghijklmnopqrstabcdefghijklmnopqrst
 This line has 30 characters it
 This line has 40 characters it a b a b a
 This line has 50 characters it a b a b a as d f e
 This line has 60 characters it a b a b a as d f e we r t yy 
 This line has 70 characters it a b a b a as d f e we r t yy e 4 r t 5 
</cfsavecontent>

<pre><cfoutput>#wrap(seventytwo,72)#

#rereplace(seventytwo,"([^\n]{1,71})[\s]","\1<br>","ALL")#</cfoutput></pre>

<cfsavecontent variable="seventytwo">
Contrary to popular belief,
  Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</cfsavecontent>

<pre><cfoutput>#rereplace(seventytwo,"([^\n]{1,71})[\s]","\1<br>","ALL")#</cfoutput></pre>

编辑:上面没有说明没有空格的72个以上字符串。你可以使用这样的东西。

<cfsavecontent variable="seventytwo">
Contrary to popular belief,
  Lorem Ipsum is not simply random text. cookiesarethebestbuttheycanmakeyoufatandnoonelikesbeingcalledfatsodonteatlotsofcookies It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</cfsavecontent>

<pre><cfoutput>#rereplace(seventytwo,"(([^\n]{1,71})[\n ]|([^\n]{1,71}))","\1<br>","ALL")#</cfoutput></pre>