ColdFusion - What is the best way to replace values to format a paragraph?
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:
- Wrap the entire block into 开发者_开发技巧a p tag.
- Replace the first occurrence of @@ with "" (nothing).
- Replace every other occurrence of @@ with
</p><p>
Is this the right way of accomplishing this task?
REReplace(your text, "@@([^@]+)", "<p>\1</p>", "all");
should sort it!
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 ...)
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>');
}
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>');
}
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
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.
精彩评论