Mimicking PHP's String Formatting Functions - str_word_count and ucfirst
(Page 2 of 4 )
Let me explain the str_word_count function: It counts words.
Yes, simple as that, you feed it a string, and it feeds you back the number of separate words within the string. Here's how we could duplicate it in ASP:
'==================================================
Function strWordCount( strToCount )
'==================================================
Dim arWords
strToCount = Trim( strToCount & "" )
arWords = split( strToCount, " " )
strWordCount = uBound( arWords ) + 1
End Function
Yes, it is really very simple. It just breaks the string by its spaces, as spaces logically define the separation of words. These are broken into an array, and the array is counted to see how many words it's holding. Of course we need to add one to the number, as arrays are zero-based, and we're not.
This will even work if you hand it a string with no spaces. It will not break, the array will contain the entire string as the first item of the array, and the word count will be one. Also, the first line of the function prevents any runtime errors due to handing the function a null value, and it does this by turning it into a string.
So that was simple, let's move on to the next two.
Sometimes it is very important to have proper formatting in a string. One example is when a user enters their City into a form, you may want to match it to a city on your database. It is essential to ensure that you establish and enforce proper formatting. In most cases it would be to have the first letter capitalized, and the rest lowercase. Yes, we can prevent problems caused by the angry people who type in all caps, or the lazy ones like myself who never hit the shift key!
PHP has a great little function call uc_first, which returns a string formatted exactly as I described. Here's a simple way to do exactly the same thing with ASP.
'===================================================
Function ucFirst( strWord )
'===================================================
strWord = trim( strWord & "" )
if len( strWord ) > 0 then
ucFirst = uCase( left( strWord, 1 ) ) & _
lcase( right( strWord, len( strWord ) - 1 ) )
end if
End Function
Going a step further, it was realized that you may have a string containing many words, and you want them ALL to be formatted in this fashion. A good example of this is a title. No matter how a title has been entered, you still want it to look like a title, and so we shall:
'===================================================
Function ucWords( strWords )
'===================================================
strWords = trim( strWords & "" )
if len( strWords ) > 0 then
dim arWords, i, strFormatted
arWords = split( strWords, " " )
for i = 0 to uBound( arWords )
strFormatted = strFormatted & " " & ucFirst( arWords( i ) )
next
ucWords = strFormatted
end if
End Function
So if you handed this function a string like this:
response.write( "i dON'T liKe thiS ARTICLE At all" )
It would come back formatted properly, and the output should be:
"I Love This Article"
Hmm, wishful thinking... if only my functions were that smart! Well then, let's move on.
Next: strip_tags >>
More ASP Articles
More By Justin Cook