如何更正 HTML 文本换行

How to correct HTML text wrapping

这是一个基本的 html 文本换行问题,但它让我感到难过。我在后端使用 coldfusion。我有以下模板:

 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 name="Report" cols="72" rows="40">#template#</textarea>

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

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

然后我将报告作为文本变量保存在 sql 数据库中。 但是当我尝试检索报告并输出包装设置为 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 

我为此绞尽脑汁,非常感谢任何建议或评论。

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

你有几个选择。

最简单的解决方案是用这样的替换来显示您的数据。

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

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

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

如果您决定保留这种效果,还有其他可能有效的方法,但请注意,它不考虑标签。虽然设计确实考虑了查找 space 或换行符,但如果您有一些像 <input type=" 这样的文本,并且输入恰好超过阈值,您最终会在 [=31] 处中断=].

#rereplace(variable,"([^\n]{1,71})[\s]","<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]","<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]","<br>","ALL")#</cfoutput></pre>

编辑:以上不包含没有 spaces 的 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}))","<br>","ALL")#</cfoutput></pre>