11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 9 • STRINGS AND REGULAR EXPRESSIONSwww.it-ebooks.inforepeat much of the information in the <strong>PHP</strong> documentation. This section is devoted to a categorical FAQof sorts, focusing upon the string-related issues that seem to most frequently appear within communityforums. The section is divided into the following topics:• Determining string length• Comparing two strings• Manipulating string case• Converting strings to and from HTML• Alternatives for regular expression functions• Padding and stripping a string• Counting characters and wordsDetermining the Length of a StringDetermining string length is a repeated action within countless applications. The <strong>PHP</strong> function strlen()accomplishes this task quite nicely. This function returns the length of a string, where each character inthe 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:In this case, the error message will not appear because the chosen password consists of tencharacters, whereas the conditional expression validates whether the target string consists of less thanten characters.Comparing Two StringsString comparison is arguably one of the most important features of the string-handling capabilities ofany language. Although there are many ways in which two strings can be compared for equality, <strong>PHP</strong>provides four functions for performing this task: strcmp(), strcasecmp(), strspn(), and strcspn().Comparing Two Strings Case SensitivelyThe strcmp() function performs a binary-safe, case-sensitive comparison of two strings. Its prototypefollows:206

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!