Regularexpression (php function)-yanying12138 1. regular expression is a string search and matching tool.
2. Common regular expression functions in php
- Preg_match ($ pattern, $ subject)
- Preg_match_all ($ pattern, $ subject, array & $ matches)
- Preg_replace ($ pattern, $ replacement, $ subject)
- Preg_filter ($ pattern, $ replacement, $ subject)
- Preg_grep ($ pattern, array $ input)
- Preg_split ($ pattern, $ subject)
- Preg_quote ($ str)
3. php function description
$ Pattern = Regular expression
$ Subject = matched target function
(1) preg_match () and preg_match_all (): Number of times that return matches the result
- Preg_match ($ pattern, $ subject, [array & $ matches]): match only once. The result is 0 or 1. the third parameter is not required. The third parameter indicates the address reference.
- Preg_match ($ pattern, $ subject, array & $ matches): returns 0, 1, 2 ......
Eg:
$ Pattern = '/[0-9]/';
$ Subject = 'weuyr3ui76as83s0ck9 ';
$ M1 = $ m2 = array ();
T1 = grep_match ($ pattern, $ subject, $ m1 );
T2 = grep_match_all ($ pattern, $ subject, $ m2 );
Result: m1 = array ([0] => 3)
M2 = array ([0] => array ([0] => 3, [1] => 7, [2] => 6, [3] => 8, [4] => 3, [5] => 0, [6] => 9 ))
T1 = 1
T2 = 7
(2) preg_replace and preg_filter: Array replacement is supported.
- Preg_replace ($ pattern, $ replacement, $ subject): retains the values that are replaced or not replaced.
- Preg_filter ($ pattern, $ replacement, $ subject): retain the replaced value.
Eg one:
$ Pattern = '/[0-9]/';
$ Subject = 'weuyr3ui76as83s0ck9 ';
$ Replacement = 'ying ';
$ Str1 = preg_replace ($ pattern, $ replacement, $ subject );
$ Str2 = preg_filter ($ pattern, $ replacement, $ subject );
Result:
$ Str1 = 'weuyr Ying ui Ying as Ying s Ying ck Ying'
$ Str2 = 'weuyr Ying ui Ying as Ying s Ying ck Ying'
Eg two:
$ Pattern = array ('/[0123]/', '/[456]/', '/[789]/')
$ Replacement = array ('AH', 'LA', 'day ')
Result:
$ Str1 = 'weuyr, ui, as, s, ck'
$ Str2 = 'weuyr, ui, as, s, ck'
Eg three:
$ Subject = array ('weuy', 'r3ui', '76as83 ', 'S', '0ck9 ');
Result:
$ Str1 = array ([0] => weuy, [1] => r, ui, [2] => as, [3] => s, [4] => Ah ck)
$ Str2 = array ([1] => r, ui, [2] => as, [4] => Ah ck)
(3) grep_grep ($ pattern, array $ input): castrated grep_filter (). only matching is performed without replacement.
Eg:
$ Pattern = '/[0-9]/';
$ Subject = array ('weuy', 'r3ui', '76as83 ', 'S', '0ck9 ');
$ Arr = preg_grep ($ pattern, $ subject );
Result:
$ Arr = array ([1] => r3ui, [2] => 76as83, [4] => 0ck9)
(4) grep_split ($ pattern, $ subject): explode is a subset of the function.
Eg:
$ Pattern = '/[0-9]/';
$ Subject = 'Hello! 3! '
$ Arr = preg_split ($ pattern, $ subject );
Result:
$ Arr = ([0] => you, [1] => good, [2] => Ah !)
(5) grep_quote ($ str): regular operator escape
Eg:
$str = 'asgs{kkk}[123]' $str = grep_quote($str)
Result:
Asgs \ {kkk \} \ [123 \]