In 2002 I wrote a CF custom tag to convert a raw text coming from a database table column into a HTML paragraph by replacing all tabs and carriage returns with “&nbsp;” and “<br>”.

Recently I needed to use that custom tag again and decided to transform it into a CF function named:
rpParagraphFormat()
Download the function here.

Arguments:

  • text (required): source text to be converted
  • tab (optional): number of spaces to replace each tab found, default 4

Function call:
#rpParagraphFormat(text=myQuery.myText, tab=3)#
#rpParagraphFormat(text=myText)#
Here is the function:

// Author: Ricardo Parente
// Date: Jul 22 2002
function rpParagraphFormat() {
var tab = "";
var tabSp = 4;
var result = "";
if (not structKeyExists(arguments,"text")) return "Text to convert not passed!";
if (structKeyExists(arguments,"tab")) tabSp = arguments.tab;
for (i=1; i lte tabSp; i=i+1){
tab = tab & "&nbsp" ;
}
result = replaceNoCase(arguments.text, chr(9), tab, "all");
result = paragraphFormat(URLDecode(replaceNoCase(URLEncodedFormat(result),"%0D%0A", "<br />", "all")));
// clear the last tag "p" inserted byparagraphFormat
if (right(result, 3) eq "<p>") {
result = left(result, len(result) -3);
}
return result;
}

4 thoughts on “Converting Raw Text into Paragraphs with Tabs and CRLF's

  1. Hi Ricardo, hope you don’t mind but I’ve modified your function to make it return tags for double carriage returns and tags for single carriage returns.

    It’s also worth noting that you need to use   (with a semi colon) to make it work on firefox.

    Here’s my updated version:

    function rpParagraphFormat() {
    var tab = “”;
    var tabSp = 4;
    var result = “”;

    if (not structKeyExists(arguments,”text”)) {
    return “Text to convert not passed!”;
    }
    else {
    tab = RepeatString(” “, tabSp);
    result = replaceNoCase(Trim(arguments.text), chr(9), tab, “all”);
    result = Replace(result, chr(13)&chr(10)&chr(13)&chr(10), “”, “all”);
    result = “” & Replace(result, chr(13)&chr(10), “”, “all”) & “”;
    return result;
    }
    }

  2. Eek – My function has been mangled! I’ll try again 🙂

    function HtmlFormatText() {
    var tab = “”;
    var tabSp = 4;
    var result = “”;

    if (not structKeyExists(arguments,”text”)) return “Text to convert not passed!”;

    tab = RepeatString(“&nbsp;”, tabSp);

    result = replaceNoCase(Trim(arguments.text), chr(9), tab, “all”);
    result = Replace(result, chr(13)&chr(10)&chr(13)&chr(10), “</p><p>”, “all”);
    result = “<p>” & Replace(result, chr(13)&chr(10), “<br />”, “all”) & “</p>”;
    return result;
    }

  3. Hi John, thanks for the correction.
    You’re always welcome to check out and comment my posts. That’s what makes our community strong, everybody’s always learning.
    Again, thanks.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.