Thestrspn()function returns the length of the first segment in a string containing characters also found in another string. Its prototype follows: int strspn(string str1, string str2) Here’s how you might usestrspn()to ensure that a password does not consist solely of numbers: <?php In this case, the error message is returned because$passworddoes indeed consist solely of digits. Calculating the Difference Between Two Strings Thestrcspn()function returns the length of the first segment of a string containing characters not found in another string. Its prototype follows: int strcspn(string str1, string str2) Here’s an example of password validation usingstrcspn(): <?php In this case, the error message will not be displayed because$passworddoes not consist solely of numbers. Manipulating String Case Four functions are available to aid you in manipulating the case of characters in a string: strtolower(), strtoupper(), ucfirst(), and ucwords(). These functions are discussed in this section. Converting a String to All Lowercase Thestrtolower()function converts a string to all lowercase letters, returning the modified string. Nonalphabetical characters are not affected. Its prototype follows: string strtolower(string str) The following example usesstrtolower()to convert a URL to all lowercase letters: <?php This returns the following: -------------------------------------------- Converting a String to All Uppercase Just as you can convert a string to lowercase, you can convert it to uppercase. This is accomplished with the functionstrtoupper(). Its prototype follows: string strtoupper(string str) Nonalphabetical characters are not affected. This example usesstrtoupper()to convert a string to all uppercase letters: <?php This returns the following: -------------------------------------------- Capitalizing the First Letter of a String Theucfirst()function capitalizes the first letter of the stringstr, if it is alphabetical. Its prototype follows: string ucfirst(string str) Nonalphabetical characters will not be affected. Additionally, any capitalized characters found in the string will be left untouched. Consider this example: <?php This returns the following: -------------------------------------------- Note that while the first letter is indeed capitalized, the capitalized word PHP was left untouched. Capitalizing Each Word in a String Theucwords()function capitalizes the first letter of each word in a string. Its prototype follows: string ucwords(string str) Nonalphabetical characters are not affected. This example usesucwords()to capitalize each word in a string: <?php This returns the following: -------------------------------------------- Note that if O’Malley was accidentally written as O’malley,ucwords()would not catch the error, as it considers a word to be defined as a string of characters separated from other entities in the string by a blank space on each side. Please check back next week for the continuation of this article.
blog comments powered by Disqus |
|
|
|
|
|
|
|