PHP18: data_valid_fns.php read regular expressions this chapter introduces regular expressions.
Let's take a look at the code of data_valid_fns.php.
1 2
3 function filled_out ($ form_vars)
4 {
5 // test that each variable has a value
6 foreach ($ form_vars as $ key => $ value)
7 {
8 if (! Isset ($ key) | ($ value = ''))
9 return false;
10}
11 return true;
12}
13
14 function valid_email ($ address)
15 {
16 // check an email address is possibly valid
17 if (ereg ('^ [a-zA-Z0-9 _ \. \-] + @ [a-zA-Z0-9 \-] + \. [a-zA-Z0-9 \-\.] + $', $ address ))
18 return true;
19 else
20 return false;
21}
22
23?>
24
A brief introduction.
Filled_out ($ form_vars) verifies the input validity.
Valid_email ($ address) verifies the validity of the email. It is implemented through a regular expression.
This chapter describes the regular expressions.
PHP supports POSIX extensions and regular expressions compatible with Perl syntax.
If you are not clear about the regular expression, see the regular expression syntax.
The following describes the regular expressions in the following two formats:
1 POSIX regular expression function
To use this function, you must first configure PHP to support it. If it is installed in Windows, it is automatically configured. However, in other operating systems, manual configuration is required.
To activate regexp support, add -- with-regex [= TYPE] when configuring PHP. TYPE can be system, apache, or php. Php is used by default.
Supported functions include:
Ereg_replace -- Regular expression replaces ereg -- Regular expression matches eregi_replace -- case-insensitive regular expression replaces eregi -- case-insensitive regular expression matches split -- splits strings into arrays using regular expressions -- use a regular expression to split a string into an array using case insensitive SQL _regcase -- generate a regular expression for matching with no size difference
2 Perl-compatible regular expression functions
Preg_grep -- returns the array unit preg_match_all that matches the pattern -- matches the global regular expression with preg_match -- matches the regular expression with preg_quote -- escapes the regular expression character preg_replace_callback -- searches and replaces the regular expression with the callback function. preg_replace -- execute regular expression search and replace preg_split -- use regular expression to split string