Both ASP and PHP have handy functions for forcing characters in a string to be all uppercase or all lowercase, but what about proper case? Proper case is used for names of people, titles of songs or movies, etc. The first letter in each word is capitalized, and the other letters are lowercase.
PHP has a built-in function for this, but the usage is slightly different and the name of the function is inconsistent, so we're going to write a facade function.
PHP
function strtoproper($someString)
{
return ucwords(strtolower($someString));
}
ASP doesn't have a function for this, though full-blown Visual Basic does. We're going to duplicate that functionality, and make a few improvements. Consider names like McDonald and O'Brien. An ordinary proper case function would not handle these properly. We're going to use a regular expression to find these situations and handle them appropriately.
ASP
function PCase(someString)
dim position
dim space
dim result
dim regEx
position = 1
set regEx = new RegExp
regEx.Pattern = "^(Mc[A-Z]{1}[A-Za-z]|O\'[A-Z]{1}[A-Za-z]|Mac[A-Z]{1}[A-Za-z])"
' Loop through the string checking for spaces.
do while InStr(position, someString, " ", 1) <> 0
' Find the position of the next space.
space = InStr(position, someString, " ", 1)
' Capitalize (and append to our output) the first character after the space which was handled by the previous run through the loop.
result = result & UCase(Mid(someString, position, 1))
' Check for situations like McDonald or O'Brien.
if not regEx.Test(Mid(someString, position, space - position)) then
' Lowercase (and append to our output) the rest of the string up to and including the current space.
result = result & LCase(Mid(someString, position + 1, space - position))
else
if Left(Mid(someString, position), 3) = "Mac" then
' Leave the next three characters intact.
result = result & Mid(someString, position + 1, 3)
' Append the rest of the string.
result = result & LCase(Mid(someString, position + 4, space - position + 4))
else
' Leave the next two characters intact.
result = result & Mid(someString, position + 1, 2)
' Append the rest of the string.
result = result & LCase(Mid(someString, position + 3, space - position + 3))
end if
end if
position = space + 1
loop
' Capitalize the first character of the last word after the final space (or the only word if there were no spaces).
result = result & UCase(Mid(someString, position, 1))
' Check for situations like McDonald or O'Brien.
if not regEx.Test(Mid(someString, position)) then
' Lowercase (and append to our output) the rest of the string up to and including the current space.
result = result & LCase(Mid(someString, position + 1))
else
if Left(Mid(someString, position), 3) = "Mac" then
' Leave the next three characters intact.
result = result & Mid(someString, position + 1, 3)
' Append the rest of the string.
result = result & LCase(Mid(someString, position + 4))
else
' Leave the next two characters intact.
result = result & Mid(someString, position + 1, 2)
' Append the rest of the string.
result = result & LCase(Mid(someString, position + 3))
end if
end if
set regEx = Nothing
PCase = result
end function
No comments:
Post a Comment