PHP正则表达式函数集锦:10个最常用函数实现灵活匹配
正则表达式是一个强大的模式匹配工具,它可以用来处理文本的输入、输出、验证和分析等方面的任务。在 PHP 中,也提供了丰富的正则表达式函数,包括 preg_match、preg_replace、preg_split 等等。下面是 PHP 正则表达式函数的集锦,介绍了最常用的10个函数以及它们的使用方法。
#### 1. preg_match
preg_match 函数用于匹配一个字符串中的模式。使用正则表达式的语法来匹配字符串,并返回一个布尔值表示是否匹配成功。
preg_match($pattern, $subject, $matches);
其中,$pattern 为正则表达式模式,$subject 为需要匹配的字符串,$matches 为用于存储匹配结果的数组。
示例:
$pattern = '/\d+/';
$subject = 'The price is $15.99';
if(preg_match($pattern, $subject, $matches)){
echo 'The price is: ' . $matches[0];
}
输出:The price is: 15
#### 2. preg_replace
preg_replace 函数用于在字符串中搜索并替换匹配的模式。使用正则表达式的语法来匹配字符串,并将匹配的字符串替换为指定的新字符串。
preg_replace($pattern, $replacement, $subject);
其中,$pattern 为正则表达式模式,$replacement 为需要替换的字符串,$subject 为目标字符串。
示例:
$pattern = '/world/'; $replacement = 'PHP'; $subject = 'Hello, world!'; echo preg_replace($pattern, $replacement, $subject);
输出:Hello, PHP!
#### 3. preg_split
preg_split 函数用于使用正则表达式模式将字符串分割为数组。
preg_split($pattern, $subject, $limit);
其中,$pattern 为正则表达式模式,$subject 为目标字符串,$limit 为指定分割数组元素数量。
示例:
$pattern = '/\s+/'; $subject = 'Hello world! How are you?'; print_r(preg_split($pattern, $subject));
输出:Array ( [0] => Hello [1] => world! [2] => How [3] => are [4] => you? )
#### 4. preg_filter
preg_filter 函数用于在字符串中搜索并替换匹配的模式,与 preg_replace 函数不同的是,它将匹配的结果用于另一个字符串处理函数中。
preg_filter($pattern, $replacement, $subject);
其中,$pattern 为正则表达式模式,$replacement 为需要替换的字符串,$subject 为目标字符串。
示例:
$pattern = '/\s+/';
$replacement = '-';
$subject = 'Hello world! How are you?';
echo implode(' ', preg_filter($pattern, $replacement, explode(' ', $subject)));
输出:Hello-world!-How-are-you?
#### 5. preg_grep
preg_grep 函数用于返回一个包含匹配模式的数组。
preg_grep($pattern, $input, $flags);
其中,$pattern 为正则表达式模式,$input 为目标数组,$flags 为需要引用的数组特性。
示例:
$pattern = '/^[a-z]$/i'; $input = ['a', 'b', 'C', 'd']; print_r(preg_grep($pattern, $input));
输出:Array ( [0] => a [1] => b [2] => C [3] => d )
#### 6. preg_last_error
preg_last_error 函数用于返回上一次 preg 函数调用的错误代码。
preg_last_error();
示例:
preg_match('/foo/', 'bar');
echo preg_last_error();
输出:2
#### 7. preg_quote
preg_quote 函数用于转义一个字符串中的特殊字符。
preg_quote ($str, $delimiter);
其中,$str 为要转义的字符串,$delimiter 为指定的定界符。
示例:
$str = 'one (two) three';
$delimiter = '/';
echo preg_match("/" . preg_quote("(", $delimiter) . "/", $str);
输出:1
#### 8. preg_match_all
preg_match_all 函数用于在字符串中找到匹配的所有子串。
preg_match_all($pattern, $subject, $matches, $flags, $offset);
其中,$pattern 为正则表达式模式,$subject 为目标字符串,$matches 为需要存储匹配结果的数组,$flags 为标志位选项,$offset 为搜索开始位置默认为 0。
示例:
$pattern = '/\d+/'; $subject = 'The price is $15.99 and $19.99'; preg_match_all($pattern, $subject, $matches); print_r($matches[0]);
输出:Array ( [0] => 15 [1] => 99 [2] => 19 [3] => 99 )
#### 9. preg_replace_callback
preg_replace_callback 函数用于在字符串中搜索并替换匹配的模式,并在每次替换后调用一个回调函数。
preg_replace_callback($pattern, $callback, $subject);
其中,$pattern 为正则表达式模式,$callback 为需要回调的函数,$subject 为目标字符串。
示例:
$pattern = '/\d+/';
$subject = 'The price is $15.99 and $19.99';
echo preg_replace_callback($pattern, function($matches){
return '$' . number_format($matches[0], 2);
}, $subject);
输出:The price is $15.99 and $19.99
#### 10. preg_split_callback
preg_split_callback 函数用于通过正则表达式模式将字符串分割成数组,并在每次分割后调用一个回调函数。
preg_split_callback($pattern, $callback, $subject, $limit);
其中,$pattern 为正则表达式模式,$callback 为需要回调的函数,$subject 为目标字符串,$limit 为限制数组元素的数量。
示例:
$pattern = '/\s+/';
$subject = 'Hello world! How are you?';
echo implode(' ', preg_split_callback($pattern, function($match){
return strrev($match[0]);
}, $subject, -1));
输出:olleH !dlrow woH era uoy?
总结:PHP 正则表达式函数是非常强大的,它可以用来解决各种文本处理问题。以上是最常用的10个 PHP 正则表达式函数,这些函数可以让你使用正则表达式实现灵活的匹配,提高你的代码效率和编码速度。
