Analysis of Perl regular expressions in PHP implementation
Perl regular expressions in PHP implementation to achieve, we want to use the PCRE regular expression functions related, then what are the specific function? Here we introduce to you four, I hope for your help.
Perl regular expressions in PHP functions used in the implementation description:
1, preg_match:
function format:
- int preg_match (string pattern, string subject, array [matches]);
This function will use the pattern in the string expression to match, if given the [regs], the string will be recorded in the [regs] [0] in, [regs] [1] on behalf of parentheses “()” Record down the first string, [regs] [2] on behalf of record of the second string, and so on. preg if found in string matching pattern, it will return “true”, otherwise “false”.
2, preg_replace:
function format:
- mixed preg_replace (mixed pattern, mixed replacement, mixed subject);
This function uses the string that matches the expression pattern of all the replaced string expression replacement. If you need replacement parts containing a pattern of characters, you can use “()” to record, just need to use in replacement of “\ \ 1″ to read.
3, preg_split:
function format:
- array preg_split (string pattern, string subject, int [limit]);
split this function and function the same, the difference with the split can be used only in a simple regular expression to split the string matched, and preg_split using full Perl-compatible regular expression type. The third parameter limit the number of representatives allowed to return eligible value.
4, preg_grep:
function format:
- array preg_grep (string patern, array input);
Basically this function and the preg_match function, but given the array preg_grep can input all the elements match, Returns a new array.
following an example, such as we have to check the Email address in the correct format:
Perl regular expressions in PHP implementation instance:
- php
- function emailIsRight ( $ email ) {
- if (preg_match (“^[_ \ .0-9a-z
- 0-9a-z] [0-9a- z-] + \.) + [az] {2,3 }$”, $ email )) {
- return 1;
- }
- return 0;
- }
- if (emailIsRight (\ ‘y10k@963.net \’)) echo \ ‘right
\ ‘ ;- if (! emailIsRight (\ ‘y10k @ fffff \’)) echo \ ‘is not correct
\’ ;- ?>
above program will output “correct
not correct.”
Perl regular expressions in PHP implementation of relevant content to introduce to you here, you want to understand and learn Perl regular expressions in PHP implementation help.