Home > Backend Development > PHP Problem > What are regular expression functions in PHP? (detailed instructions for use)

What are regular expression functions in PHP? (detailed instructions for use)

慕斯
Release: 2023-03-10 13:14:01
Original
2130 people have browsed it

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)

What are regular expression functions in PHP? (detailed instructions for use)

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,&#39;abc&#39;,&#39;2&#39;,&#39;c3c&#39;,&#39;d&#39;);
var_dump($arr);
$pattern = &#39; /\d/&#39; ;
$result = preg_grep($pattern,$arr ,0);
var_dump($result);
?>
Copy after login

Code running results:

What are regular expression functions in PHP? (detailed instructions for use) Let’s continue to understand (the function that performs a regular match) preg_match. We define the regular expression $pattern,

<?php
//preg_ match() 进行一次正则匹配
$pattern=&#39;/\w/&#39;;
$str = &#39;!@a#$b%^c&*d(&#39; ;
$result = preg_match($pattern, $str,$match);
var_dump($result);
var_dump($match);
?>
Copy after login

The code running result:

What are regular expression functions in PHP? (detailed instructions for use)

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=&#39;/\w/&#39;;
$str = &#39;!@a#$b%^c&*d(&#39; ;
$result = preg_match_all($pattern, $str,$match);
var_dump($result);
var_dump($match);
?>
Copy after login

The result of running the code:

What are regular expression functions in PHP? (detailed instructions for use)

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template