ColdFusion - 替换值以格式化段落的最佳方法是什么?

发布于 2024-12-03 18:58:50 字数 668 浏览 1 评论 0原文

我正在使用 ColdFusion 9。

我将数据库中的一段文本直接输出到网页中。我不希望用户必须编写 HTML 并把事情搞砸。因此,我让他们插入一个双符号“@@”来表示一个新段落。数据是这样存储的:

 @@This is my first paragraph. @@This will be my second paragraph. @@This is my third.

我想要做的是创建一个函数,将上面的文本块转换成下面的文本块:

 <p>This is my first paragraph.</p>
 <p>This will be my second paragraph.</p>
 <p>This is my third.</p>

我永远不会知道用户是否实际上使用了任何 @@ 符号。如果没有,我需要将整个块包装在 ap 标记中。

我的第一个想法是这样做:

  1. 将整个块包装到 ap 标签中。
  2. 将第一次出现的@@ 替换为“”(无内容)。
  3. 将所有其他出现的 @@ 替换为

这是完成此任务的正确方法吗?

I am using ColdFusion 9.

I am outputting a chunk of text from a database to be directly into a web page. I don't want users to have to write HTML and screw things up. So, I am having them insert a double at sign "@@" to denote a new paragraph. Data is stored like this:

 @@This is my first paragraph. @@This will be my second paragraph. @@This is my third.

What I want to do is create a function that will turn the above block of text into the block of text below:

 <p>This is my first paragraph.</p>
 <p>This will be my second paragraph.</p>
 <p>This is my third.</p>

I will never know whether the user actually used any @@ signs or not. If they didn't, I'll need to wrap the entire block in a p tag.

My first thought is to do this:

  1. Wrap the entire block into a p tag.
  2. Replace the first occurrence of @@ with "" (nothing).
  3. Replace every other occurrence of @@ with </p><p>

Is this the right way of accomplishing this task?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

绅士风度i 2024-12-10 18:58:50

REReplace(您的文字, "@@([^@]+)", "

\1

", "all");

应该排序吧!

REReplace(your text, "@@([^@]+)", "<p>\1</p>", "all");

should sort it!

酒绊 2024-12-10 18:58:50

如果您致力于避免使用 HTML(这并不是一个坏主意),并且您不想使用 Henry 的文本区域和 ParagraphFormat() 的想法,那么我建议使用 ' @@' 代表段落中断。不需要在文本前面加上它,因为它们总是至少有一段文本,对吧? (如果他们什么都不输入,则不需要任何段落。)

除此之外,是的,只需将 @@ 替换为

或简单的

就可以了...如果您不需要它们用它来开始文本,那么您的替换过程会更容易,因为您可以跳过步骤 2,并且如果你可以只使用

不关闭它,那么您也可以跳过步骤 1。 (如果您尝试创建 XHTML,那么您需要使用结束标记,但是处理每个段落的文本时您将面临更困难的任务......)

If you are committed to avoiding HTML (which isn't such a bad idea), and you'd rather not use Henry's idea of a textarea and ParagraphFormat(), what I'd recommend is using '@@' to represent a paragraph break. There's no need to prefix text with it, because they're always going to have at least one paragraph of text, right? (And if they enter nothing at all, you don't need any paragraphs.)

Aside from that, yes, simply replacing @@ with </p><p> or simply <p> is fine ... if you don't need them to start the text with it, then your replacement process is easier, because you can skip step 2, and if you can use just <p> without closing it, then you can skip step 1 as well. (If you were trying to create XHTML, then you'd need to use closing tags, but then you'd have a more difficult task handling the text for each paragraph ...)

傲性难收 2024-12-10 18:58:50

你可以尝试这样的事情:

testParagraph = '@@First graf. @@Second graf. @@Third graf.';
arPara = testParagraph.split('@@');
for( i = 2; i <= arrayLen(arPara); i++ ){ // start at 2 to avoid blank graf
  writeoutput('<p>' & arPara[i] & '</p>');
}

You could try something like this:

testParagraph = '@@First graf. @@Second graf. @@Third graf.';
arPara = testParagraph.split('@@');
for( i = 2; i <= arrayLen(arPara); i++ ){ // start at 2 to avoid blank graf
  writeoutput('<p>' & arPara[i] & '</p>');
}
辞别 2024-12-10 18:58:50

我喜欢 Ken 在“@@”上分裂的方法。但是,我不会简单地从第二个数组项开始。用户可能没有意识到他们需要将@@放在第一段之前,或者他们可能认为他们需要将其放在最后一段之后。我会将其拆分,然后测试每个段落以确保它不为空。如果用户输入正确,第一个将为空并且不显示。如果他们不这样做,就不算犯规。

testParagraph = '@@First graf. @@Second graf. @@Third graf.';
arPara = testParagraph.split('@@');
for( i = 1; i <= arrayLen(arPara); i++ ){ // start at 2 to avoid blank graf
  if (trim(arPara[i]) neq "") writeoutput('<p>' & arPara[i] & '</p>');
}

I like Ken's approach of splitting on '@@'. However, I wouldn't simply start from the second array item. Users might not realize they need to put @@ before the first paragraph, or they might think they need to put it after the last one. I would split it, then test each paragraph to make sure it's not empty. If the users enter it right, the first one will be empty and not show. If they don't, no foul.

testParagraph = '@@First graf. @@Second graf. @@Third graf.';
arPara = testParagraph.split('@@');
for( i = 1; i <= arrayLen(arPara); i++ ){ // start at 2 to avoid blank graf
  if (trim(arPara[i]) neq "") writeoutput('<p>' & arPara[i] & '</p>');
}
乖乖公主 2024-12-10 18:58:50

嗯,为什么不使用 TinyMCE [或类似的东西] 作为用户需要编辑的文本区域,这样编辑器将为您生成所有 html,而用户则不必使用 @@ 或插入 html?

然后,您仍然可以在提交到数据库之前清理用户输入,以防万一您担心邪恶的 HTML ....

-sean

UPDATE

好的 - 那么如何使用列表到数组功能,测试您的第一个数组元素确保它不为空,然后

在适当的位置使用“ ”循环遍历数组? listToArray 的分隔符参数有效地去掉了“@@” - http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_l_21.html

Um why not use TinyMCE [or something similar] for the text area tht the users need to edit, that way the editor will generate all the html for you and users neither have to use your @@ or insert html?

You can then still sanitize the user input before committing to the DB just incase you are worried about nefarious HTML ....

-sean

UPDATE

ok - well then how about using the list to array function, testing your first array element to be sure it's not empty, then just loop over the array with the '

' in the appropriate place? The delimiters param of listToArray effectively strips out your "@@" - http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_l_21.html

绮筵 2024-12-10 18:58:50

我认为人们建议采用不同的方法来处理数据是因为您的方法不必要......呃......“不太理想”。你问“问题x”,但人们可以看到“问题x”前提中的缺陷,因此提供更好的建议来解决作为“问题x”基础的看似有缺陷的前提。我认为他们的方法是合理的,作为一个新手(我的意思是根据你在这里提出的问题来判断),对提供建议的人更加开放可能是值得的:毕竟他们不是无缘无故地这样做的!

顺便说一句,我同意人们建议你修改你的方法。

但是,要回答您的问题,请执行以下操作:

  • s = reReplace(s, "^\s*@@(.*)$", "

    \1

    ", " ONE")

  • 将 @@ 的所有实例替换为

就是这样。这里没有什么复杂的。

对于这种情况(如果不是问题的话),更好的答案是研究如何微调基于 Web 的文本编辑器(如 TinyMCE)以允许某些标记,但不允许其他标记。不管你喜欢与否,这对于你的情况来说是一个更好的方法。

I think people are suggesting different approaches to dealing with the data is because your approach is unnecessarily... err... "less than ideal". You are asking "question x", but people can see the flaw in the premise of "question x", hence offering better suggestions to address the seemingly flawed premise that was the basis of "question x". I think their approach is sound, and as a newbie (judging on the questions you ask here, I mean), it might be worthwhile being a little bit more open to people offering advice: they are not doing it for no reason, after all!

I agree with the people suggesting you revise your approach, btw.

However, to answer your question, do this:

  • s = reReplace(s, "^\s*@@(.*)$", "

    \1

    ", "ONE")

  • replace all instances of @@ with

That's it. There is nothing complex here.

A better answer to the situation - if not the question - would be to look at how web-based text editors like TinyMCE can be fine-tuned to allow some mark-up, but not others. Like it or not, this would be a better approach to your situation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文