Home > Backend Development > PHP Tutorial > Commonly used regular expression functions in PHP

Commonly used regular expression functions in PHP

WBOY
Release: 2023-06-20 08:58:01
Original
1518 people have browsed it

With the continuous development of network technology, the use of regular expressions has now become an indispensable part of web development. The PHP language also provides many powerful regular expression functions, which can help us quickly process strings and effectively improve our development efficiency. Next, let’s introduce in detail the regular expression functions commonly used in PHP.

preg_match()

preg_match() is the most basic regular expression function in PHP, which is used to match a regular expression in a string. preg_match()The syntax of the function is as follows:

int preg_match( string $pattern, string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
Copy after login

Among them, $pattern is the regular expression, $subject is the target character to be matched String, $matches is optional, used to save the matched results, $flags is optional, used to set matching options, $offset is Optional, used to set the position in the target string from which to start matching.

Use the preg_match() function to quickly determine whether a string matches a certain pattern. If it matches, return 1; otherwise, return 0.

For example, the following code can determine whether a string is a valid email address:

$email = 'john@example.com';
if (preg_match('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/', $email)) {
    echo "This is a valid email address.";
} else {
    echo "This is not a valid email address.";
}
Copy after login

preg_replace()

preg_replace()Function Is the most commonly used function for regular expression replacement in strings. Its syntax is as follows:

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

Among them, $pattern is the regular expression, $replacement is the content to be replaced, $subject is the target string, $limit is optional, indicating the maximum number of substitutions, $count is optional, used to save the number of substitutions.

For example, the following code can replace all spaces in a string with underscores:

$string = 'Hello, world!';
$new_string = preg_replace('/s+/', '_', $string);
echo $new_string;
Copy after login

preg_split()

preg_split()The function is Function used to split the target string according to regular expressions. Its syntax is as follows:

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

Among them, $pattern is the regular expression, $subject is the target string, and $limit is the Options, indicating the maximum number of times to split, $flags is optional, used to set split options.

For example, the following code can split a string according to commas and spaces:

$string = 'apple, banana, orange';
$array = preg_split('/s*,s*/', $string);
print_r($array);
Copy after login

preg_grep()

preg_grep()The function is used Function to search for elements matching a regular expression in an array. Its syntax is as follows:

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

Among them, $pattern is a regular expression, $input is the array to be searched, and $flags is Optional, used to set search options.

For example, the following code can search for all elements starting with the letter s in an array:

$array = array('apple', 'banana', 'orange', 'strawberry', 'kiwi');
$result = preg_grep('/^s/', $array);
print_r($result);
Copy after login

preg_last_error()

preg_last_error()Function Is a function used to obtain the error information of the last regular expression operation. Its syntax is as follows:

int preg_last_error( void )
Copy after login

For example, the following code can get the error code of the last regular expression operation:

$string = 'Hello, world!';
if (preg_match('/w+s+(w+)/', $string)) {
    echo "Match succeeded.";
} else {
    $error = preg_last_error();
    echo "Match failed with error code $error.";
}
Copy after login

The above is the regular expression function commonly used in PHP. In actual development, these functions can help us save a lot of time and energy and greatly improve development efficiency. Therefore, it is very necessary for a PHP developer to be proficient in these functions.

The above is the detailed content of Commonly used regular expression functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

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