Home > Backend Development > PHP Tutorial > How to use regular expression functions in PHP

How to use regular expression functions in PHP

WBOY
Release: 2023-05-18 12:22:02
Original
899 people have browsed it

In PHP, regular expressions are a very powerful tool that can be used to match patterns in text. PHP provides many built-in functions to operate regular expressions, making them more convenient and faster.

In this article, we will learn how to use regular expression functions in PHP, covering the following topics:

  1. Regular Expression Syntax
  2. preg_match function
  3. preg_match_all function
  4. preg_replace function
  5. preg_split function
  6. Some other useful functions
  7. Regular expression syntax

The syntax of regular expressions is somewhat complicated, but once you understand it, you can easily use regular expressions to search and replace text.

In PHP, the syntax of regular expressions includes the following metacharacters:

. Represents any single character
^ Represents starting with a certain character or string
$ Represents starting with The end of a certain character or string
[] represents a specified set of characters
| represents a logical OR operation
() is used to capture the matched text

  • represents the previous characters or subexpression can appear zero or multiple times
  • means that the previous character or subexpression can appear one or more times
    ? means that the previous character or subexpression can appear zero or once

In addition to these metacharacters, regular expressions also support some special character categories and escape characters. These can be found in the PHP manual.

  1. preg_match function

The preg_match function is a very commonly used function, used to search for patterns in text that match specified regular expressions. It returns the first matching string.

The following is the basic syntax of the preg_match function:

preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ] ]] ): int|false

Among them, $pattern is a regular expression and $subject is the text to be searched.

For example, we want to find the first "php" word from a string. Here is an example using preg_match:

$str = "This is php programming.";
if (preg_match("/php/", $str)) {
echo "Match found !";
} else {
echo "Match not found.";
}

Output result:

Match found!

This example , we used preg_match to search for the pattern "php" in the string $str.

  1. preg_match_all function

Different from the preg_match function, the preg_match_all function searches the text for patterns that match the specified regular expression and returns all matched strings.

The following is the basic syntax of the preg_match_all function:

preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ] ]] ): int|false

In this example, $pattern is the regular expression and $subject is the text to be searched. $matches is an array that stores all matched strings.

For example, we want to find all the numbers in a string. Here is an example using the preg_match_all function:

$str = "In 2020, I met 18 people, and in 2019, I met 10 people.";
preg_match_all("/d /", $ str, $matches);
print_r($matches);

Output result:

Array
(

[0] => Array
    (
        [0] => 2020
        [1] => 18
        [2] => 2019
        [3] => 10
    )
Copy after login

)

at In this example, we use preg_match_all to search for all numbers in the string $str, and use the print_r function to output the searched numbers.

  1. preg_replace function

The preg_replace function is used to search for a specified regular expression pattern in text and replace it with a specified string.

The following is the basic syntax of the preg_replace function:

preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) : mixed

In this example, $pattern is the regular expression, $replacement is the string to replace the matched text, and $subject is the text to be searched. $limit is the maximum number to replace.

For example, we want to remove all numbers from a string. Here is an example using the preg_replace function:

$str = "In 2020, I met 18 people, and in 2019, I met 10 people.";
$pattern = '/d /';
$replacement = '';
echo preg_replace($pattern, $replacement, $str);

Output result:

In , I met people, and in , I met people.

In this example, we use the preg_replace function to search for all numbers and replace them with empty strings.

  1. preg_split function

The preg_split function is used to split a text string based on a specified regular expression pattern.

The following is the basic syntax of the preg_split function:

preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] ): array

In this example, $pattern is the regular expression and $subject is the text string to be split. $limit is the maximum number to split.

For example, we want to split a comma-separated string into an array. Here is an example using the preg_split function:

$str = "apple,orange,banana,grape";
$array = preg_split("/,/", $str);
print_r ($array);

Output result:

Array
(

[0] => apple
[1] => orange
[2] => banana
[3] => grape
Copy after login

)

In this example, we use the preg_split function according to the comma Split a string into an array.

  1. Some other useful functions

In addition to the above functions, PHP also provides many other useful regular expression functions:

preg_grep: used to search arrays elements that match the regular expression and returns a new array containing the matching elements.

preg_filter: Used to search for elements in an array that match a regular expression and replace them with the specified replacement string.

preg_last_error: Returns the error code of the last regular expression function.

  1. Summary

The regular expression functions in PHP provide the flexibility and power of basic operations to easily manipulate text. Understanding the usage and syntax of these functions in PHP is the first step to learning regular expressions.

The above is the detailed content of How to use regular expression functions in PHP. 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