Can PHP regularity match arrays?

青灯夜游
Release: 2023-03-16 15:46:01
Original
2450 people have browsed it

php regular expression can match arrays. In PHP, you can use the preg_grep() function to match elements in an array using regular expressions; this function can search for each element in the array and return an array composed of all elements that match the regular expression. The syntax "preg_grep (regular expression, array, $flags)"; the parameter "$flags" is optional. When the value is set to "PREG_GREP_INVERT", an array consisting of elements in the array that do not match the regular expression will be returned.

Can PHP regularity match arrays?

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php regular expression can match arrays.

In PHP, when it comes to regular matching, we think of strings. In fact, in addition to matching strings, regular expressions can also be used to match arrays.

In PHP, you can use the preg_grep() function to match elements in an array using regular expressions.

preg_grep() function can search all elements in an array and return an array composed of all elements that match the regular expression. The syntax format of this function is as follows:

preg_grep($pattern, $input [, $flags = 0 ])
Copy after login

The parameter description is as follows:

  • $pattern: the pattern to be searched, which is the defined regular expression;
  • $input: Array to be searched;
  • $flags: Optional parameter, which can be set to PREG_GREP_INVERT. In this case, the function will return an array composed of elements in the array that do not match the given pattern $pattern.

perg_grep() function will traverse each element in the $input array, match the element with the pattern $pattern, and then return the elements that match successfully or fail to match.

Example 1: Return the specified matching element in the array

Copy after login

Can PHP regularity match arrays?

Example 2:

'; var_dump($preg_arr_1); var_dump($preg_arr_2); ?>
Copy after login

Can PHP regularity match arrays?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Can PHP regularity match arrays?. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!