欢迎访问宙启技术站
智能推送

PHP正则表达式实战:10个常用函数解析!

发布时间:2023-06-23 14:10:27

正则表达式是一种强大的字符串匹配工具,PHP中有许多内置函数可以使用正则表达式。本文将介绍PHP中10个常用的正则表达式函数及其使用方法。

1. preg_match()

preg_match()函数用于从字符串中查找匹配正则表达式的部分。它的用法如下:

preg_match(pattern, subject, matches)

其中,pattern是正则表达式,subject是要搜索的字符串,matches是可选参数,用于存储匹配的结果。如果能找到匹配的部分,就返回1,否则返回0。

例子:

$pattern = '/^[a-z]+$/';
$subject = 'teststring';
if (preg_match($pattern, $subject)) {
    echo 'Match found!';
} else {
    echo 'Match not found.';
}

2. preg_replace()

preg_replace()函数用于使用正则表达式替换字符串中所有匹配的部分。它的用法如下:

preg_replace(pattern, replacement, subject)

其中,pattern是正则表达式,replacement是替换字符串,subject是要搜索的字符串。

例子:

$pattern = '/red|green|blue/i';
$replacement = 'yellow';
$subject = 'The sky is blue.';
echo preg_replace($pattern, $replacement, $subject);

这段代码会将字符串中所有的"blue"替换为"yellow"。

3. preg_split()

preg_split()函数用于使用正则表达式分割字符串。它的用法如下:

preg_split(pattern, subject)

其中,pattern是正则表达式,subject是要搜索的字符串。

例子:

$pattern = '/\s+/';
$subject = 'This is a test string';
print_r(preg_split($pattern, $subject));

这段代码会以空格为分隔符将字符串分割成数组。

4. preg_match_all()

preg_match_all()函数用于从字符串中查找所有匹配正则表达式的部分。它的用法和preg_match()类似,只是它会查找到所有的匹配部分,并存储在一个数组中。

例子:

$pattern = '/\d+/';
$subject = 'I have 2 apples and 3 oranges';
preg_match_all($pattern, $subject, $matches);
print_r($matches[0]);

这段代码会找到所有的数字,并将它们存储在一个数组中。

5. preg_grep()

preg_grep()函数用于从一个数组中返回所有匹配正则表达式的元素。它的用法如下:

preg_grep(pattern, input)

其中,pattern是正则表达式,input是要搜索的数组。

例子:

$pattern = '/^[a-m]/';
$input = array('apple', 'banana', 'candy', 'donut');
$result = preg_grep($pattern, $input);
print_r($result);

这段代码会返回数组中所有以字母a到m开头的元素。

6. preg_last_error()

preg_last_error()函数用于返回最近一个正则表达式操作的错误号码。它的用法如下:

preg_last_error()

如果没有错误,则返回0,否则返回一个非0值,表示错误号码。

7. preg_quote()

preg_quote()函数用于将正则表达式中的特殊字符转义,以便字符串中的这些字符不被解释为正则表达式元字符。它的用法如下:

preg_quote(str, delimiter)

其中,str是要转义的字符串,delimiter是可选的分隔符。

例子:

$str = 'This is a test $string';
$pattern = preg_quote('$string');
echo preg_match("/$pattern/", $str);

这段代码会将字符串中的"$string"转义为"\$string",以便它不被解释为正则表达式中的特殊字符。

8. preg_replace_callback()

preg_replace_callback()函数用于使用回调函数替换字符串中所有匹配的部分。它的用法如下:

preg_replace_callback(pattern, callback, subject)

其中,pattern是正则表达式,callback是一个回调函数,用于替换匹配的部分,subject是要搜索的字符串。

例子:

function toUpperCase($matches) {
    return strtoupper($matches[0]);
}
$pattern = '/([a-z]+)/i';
$subject = 'This is a test string';
echo preg_replace_callback($pattern, 'toUpperCase', $subject);

这段代码会将字符串中所有的单词都转换为大写。

9. preg_quote()

preg_quote()函数用于将正则表达式中的特殊字符转义,以便字符串中的这些字符不被解释为正则表达式元字符。它的用法如下:

preg_quote(str, delimiter)

其中,str是要转义的字符串,delimiter是可选的分隔符。

例子:

$str = 'This is a test $string';
$pattern = preg_quote('$string');
echo preg_match("/$pattern/", $str);

这段代码会将字符串中的"$string"转义为"\$string",以便它不被解释为正则表达式中的特殊字符。

10. preg_replace_callback_array()

preg_replace_callback_array()函数用于使用回调函数数组替换字符串中所有匹配的部分。它的用法如下:

preg_replace_callback_array(callbacks, subject)

其中,callbacks是一个回调函数数组,用于替换匹配的部分,subject是要搜索的字符串。

例子:

function toUpperCase($matches) {
    return strtoupper($matches[0]);
}
$callbacks = array(
    '/([a-z]+)/i' => 'toUpperCase',
    '/\d+/' => function($matches) { return $matches[0] * 2; }
);
$subject = 'This is a test string with number 123.';
echo preg_replace_callback_array($callbacks, $subject);

这段代码会将字符串中的单词转换为大写,并将数字加倍。