The previous article introduced you to "What are the pattern modifiers in PHP? How are they used? (With detailed explanation) 》, this article will continue to introduce to you and let’s talk about the regular expression functions in PHP? How to use it (with code)
Regular expression function
preg. grep() returns the array unit that matches the pattern ( One-dimensional array)
First, we first understand the array unit (preg_prep) that returns the pattern matching, and then we start writing parameters. The first parameter @param1 (pass in the regular expression to be matched formula); the second parameter @param2 (array to be matched); the third parameter @param3 (optional parameter);
We first define an array, $arr, and then we define the regular expression Formula $pattern, then output the result (result) of (preg_grep), and finally we print out $result; when we print the results, we find that we did not print out those pure letters,
<?php $arr = array(1,'abc','2','c3c','d'); var_dump($arr); $pattern = ' /\d/' ; $result = preg_grep($pattern,$arr ,0); var_dump($result); ?>
Code running results:
Let’s continue to understand (the function that performs a regular match) preg_match. We define the regular expression $pattern,
<?php //preg_ match() 进行一次正则匹配 $pattern='/\w/'; $str = '!@a#$b%^c&*d(' ; $result = preg_match($pattern, $str,$match); var_dump($result); var_dump($match); ?>
The code running result:
The above code shows that if we want to do some queries or matching in the future, we just want to test whether there is what we want. If it is, it means success, and if it is not, it means failure.
Contrary to the function we mentioned above, there is also //preg_ match_ _all(), which performs regular matching to obtain all matching content. Copy the above code and add all after $result = preg_match to get $result = preg_match_all , the result of running the code is that all the results we input are output, and the result returns a two-dimensional array,
<?php //preg_ match() 进行一次正则匹配 $pattern='/\w/'; $str = '!@a#$b%^c&*d(' ; $result = preg_match_all($pattern, $str,$match); var_dump($result); var_dump($match); ?>
The result of running the code:
Recommended learning : "PHP Video Tutorial"
The above is the detailed content of What are regular expression functions in PHP? (detailed instructions for use). For more information, please follow other related articles on the PHP Chinese website!