0

UDF: countHtmlLineBreaks() - return the number of BR tags in a string

ColdFusion

Whenever 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.

edit:I have modified the expression a bit at Rob Wilkerson's recommendation. Thanks Rob!

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;
}


tags:
ColdFusion
todd sharp said:
 
Man I could have done that easier. :D
 
posted 981 days ago
Add Comment Reply to: this comment OR this thread
 
 
lol.... welcome "that guy"!
 
posted 981 days ago
Add Comment Reply to: this comment OR this thread
 
Rob Wilkerson said:
 
Just because I can't resist, this regex may give you more flexibility (in case you don't have control over the code you're scanning):

<\s*br\s*/?\s*>

Any space is detected and the parentheses are unnecessary. Because nothing is captured, the latter change might, just might, give you a very small performance gain.

Just wanted to make sure your streak held. :-)
 
posted 981 days ago
Add Comment Reply to: this comment OR this thread
 
todd sharp said:
 
Regular Expessions - WHO THE F named that? There is *nothing* regular about those things. I hate them. Never understood them, never will.

 
posted 981 days ago
Add Comment Reply to: this comment OR this thread
 
 
Ha! Fair enough Rob. I will alter the post and give you credit.

~Dave
 
posted 981 days ago
Add Comment Reply to: this comment OR this thread
 
Rob Wilkerson said:
 
@Dave:
That guy strikes again. The problem with being "that guy" is those times when you have a typo in your "better" solution. Or, in one of those Southwest "Want to get away" moments, when you're not as smart as you think you are and your solution doesn't actually work. Doh!

@todd:
I used to feel that way too, but now that I've become pretty proficient with them I think they're the greatest development tool *ever*. Seriously. I use them for damn near everything. I agree, however, that they're badly misnamed. :-)
 
posted 981 days ago
Add Comment Reply to: this comment OR this thread
 

Search