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.infoYou may wish to search for these special characters in strings instead of using them in the specialcontext just described. To do so, the characters must be escaped with a backslash (\). For example, if youwant to search for a dollar amount, a plausible regular expression would be as follows: ([\$])([0-9]+); thatis, a dollar sign followed by one or more integers. Notice the backslash preceding the dollar sign.Potential matches of this regular expression include $42, $560, and $3.Predefined Character Ranges (Character Classes)For reasons of convenience, several predefined character ranges, also known as character classes, areavailable. Character classes specify an entire range of characters—for example, the alphabet or aninteger set. Standard classes include the following:[:alpha:]: Lowercase and uppercase alphabetical characters. This can also bespecified as [A-Za-z].[:alnum:]: Lowercase and uppercase alphabetical characters and numerical digits.This can also be specified as [A-Za-z0-9].[:cntrl:]: Control characters such as tab, escape, or backspace.[:digit:]: Numerical digits 0 through 9. This can also be specified as [0-9].[:graph:]: Printable characters found in the range of ASCII 33 to 126.[:lower:]: Lowercase alphabetical characters. This can also be specified as [a-z].[:punct:]: Punctuation characters, including ~ ` ! @ # $ % ^ & * ( ) - _ + = { } [ ] : ; '< > , . ? and /.[:upper:]: Uppercase alphabetical characters. This can also be specified as [A-Z].[:space:]: Whitespace characters, including the space, horizontal tab, vertical tab,new line, form feed, or carriage return.[:xdigit:]: Hexadecimal characters. This can also be specified as [a-fA-F0-9].<strong>PHP</strong>’s Regular Expression Functions (POSIX Extended)<strong>PHP</strong> offers seven functions for searching strings using POSIX-style regular expressions: ereg(),ereg_replace(), eregi(), eregi_replace(), split(), spliti(), and sql_regcase(). These functionsare discussed in this section.Performing a Case-Sensitive SearchThe ereg() function executes a case-sensitive search of a string for a defined pattern, returning thelength of the matched string if the pattern is found and FALSE otherwise. Its prototype follows:int ereg(string pattern, string string [, array regs])Here’s how you could use ereg() to ensure that a username consists solely of lowercase letters:194

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

Saved successfully!

Ooh no, something went wrong!