Calculating the Difference Between Two Strings
The strcspn() 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)
252 CHAPTER 9 ?– STRINGS AND REGULAR EXPRESSIONS
Here??™s an example of password validation using strcspn():
$password = "a12345";
if (strcspn($password, "1234567890") == 0) {
echo "Password cannot consist solely of numbers!";
}
?>
In this case, the error message will not be displayed because $password does 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
The strtolower() 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 uses strtolower() to convert a URL to all lowercase letters:
$url = "http://WWW.EXAMPLE.COM/";
echo strtolower($url);
?>
This returns the following:
http://www.example.com/
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 function strtoupper().
Pages:
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330