UDF: countHtmlLineBreaks() - return the number of BR tags in a string
ColdFusionWhenever I end up creating some fairly generic little UDF, I usually try to put up here in case some random person somewhere happens to find them useful. Inevitably, I always get *that guy* who gives me grief for doing so and tells me how I should have done it better or how I should have used Xyz() udf instead, but what the heck... here is another one.
I had a need this morning to be able to set the height of a div that contained an unordered list of strings based on the text it contained. I created an algorithm that was basically Ceiling(Len(string)/CharactersPerLine) for each item in the list and then determined the pixel height based on number of lines, but run into an issue where the text could contain
tags. I needed a method for counting BR tags, and it should be able to find BR tags with any number of spaces and with or without a closing slash. This is what I came up with.
In case the code above isn't, here it is again:
function countHtmlLineBreaks(String) {
var Count = 0;
while (ReFindNoCase("<\s*br\s*/?\s*>",arguments.String)) {
Count = Count + 1;
arguments.String = ReReplaceNoCase(String,"<\s*br\s*/?\s*>","","one");
}
return Count;
}





Loading....