In addition to the regular expression–based functions discussed in the first half of this chapter, PHP offers more than 100 functions collectively capable of manipulating practically every imaginable aspect of a string. To introduce each function would be out of the scope of this book and would only repeat much of the information in the PHP documentation. This section is devoted to a categorical FAQ of sorts, focusing upon the string-related issues that seem to most frequently appear within community forums. The section is divided into the following topics:
Determining the Length of a String Determining string length is a repeated action within countless applications. The PHP function strlen() accomplishes this task quite nicely. This function returns the length of a string, where each character in the string is equivalent to one unit. Its prototype follows: int strlen(string str) The following example verifies whether a user password is of acceptable length: <?php In this case, the error message will not appear because the chosen password consists of ten characters, whereas the conditional expression validates whether the target string consists of less than ten characters. Comparing Two Strings String comparison is arguably one of the most important features of the string-handling capabilities of any language. Although there are many ways in which two strings can be compared for equality, PHP provides four functions for performing this task: strcmp(), strcasecmp(),strspn(), andstrcspn(). These functions are discussed in the following sections. Comparing Two Strings Case Sensitively Thestrcmp()function performs a binary-safe, case-sensitive comparison of two strings. Its prototype follows: int strcmp(string str1, string str2) It will return one of three possible values based on the comparison outcome:
Web sites often require a registering user to enter and then confirm a password, lessening the possibility of an incorrectly entered password as a result of a typing error.strcmp()is a great function for comparing the two password entries because passwords are often case sensitive: <?php if (strcmp($pswd,$pswd2) != 0) Note that the strings must match exactly forstrcmp()to consider them equal. For example,Supersecretis different fromsupersecret. If you’re looking to compare two strings case insensitively, considerstrcasecmp(), introduced next. Another common point of confusion regarding this function surrounds its behavior of returning0if the two strings are equal. This is different from executing a string comparison using the==operator, like so: if ($str1 == $str2) While both accomplish the same goal, which is to compare two strings, keep in mind that the values they return in doing so are different. Comparing Two Strings Case Insensitively Thestrcasecmp()function operates exactly likestrcmp(), except that its comparison is case insensitive. Its prototype follows: int strcasecmp(string str1, string str2) The following example compares two e-mail addresses, an ideal use forstrcasecmp()because case does not determine an e-mail address’s uniqueness: <?php if (! strcasecmp($email1, $email2)) In this example, the message is output becausestrcasecmp()performs a case-insensitive comparison of$email1and$email2and determines that they are indeed identical.
blog comments powered by Disqus |
|
|
|
|
|
|
|