Skip to main content
Inspiring
June 6, 2008
Question

Beginners UDFs

  • June 6, 2008
  • 1 reply
  • 354 views
Hi all,
What do you think of this udf? How can I make it better have I done it ok? Thanks for any input
This topic has been closed for replies.

1 reply

Inspiring
June 6, 2008
To make it better:

1. use a cffunction tag. In this specific case it might not make much difference, but it's a good habit to adopt as the cfargument tag is pretty useful.

2. This is wrong
var xvali = ReReplace(info,"<P>","<p>","all");
because there is no variable in your function called info.

3. Your function would be easier to maintain (add more substutions) if you start with two lists and do a loop.

something like this

var mystring = arguments.texttoxhtml;
var goodlist = "<p>,</p>,<b>,</b>";
var badlist = "<P>,</P>,<STRONG>,</STRONG>";
var i = 1;
var listitems = ListLen(GoodList);
for (i = 1; i lte ListItems; i = i + 1) {
MyString = Replace(MyString, ListGetAt(BadList, i), ListGetAt(GoodList, i), "all");
}
</cfscript>
<cfreturn MyString>
}

That's the general idea. If I got anything wrong, you can fix it.
Inspiring
June 9, 2008
Thak you Dan I did as you said and it works great, that for cfscript loop is guaranteed to give me a headache!

for (i = 1; i lte ListItems; i = i + 1) {
MyString = Replace(MyString, ListGetAt(BadList, i), ListGetAt(GoodList, i), "all");
}

However, I do recognize the code and it seems like it looks more complicated than it is and is almost cut and paste for looping. Interestingly on my first attempt the info variable did work. Thanks Dan really appreciated. Here's the UDF!