preg_filter 执行一个正则表达式搜索和替换
发表日期:2021-07-01 08:56:55 | 来源: | | 浏览(694) 分类:PCRE 函数
preg_filter
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
preg_filter — 执行一个正则表达式搜索和替换
说明
preg_filter(
mixed
mixed
mixed
int
int
): mixed
mixed
$pattern,mixed
$replacement,mixed
$subject,int
$limit = -1,int
&$count = ?): mixed
preg_filter()等价于preg_replace() 除了它仅仅返回(可能经过转化)与目标匹配的结果. 这个函数怎样工作的更详细信息请阅读 preg_replace()文档.
返回值
如果subject是一个数组,返回一个数组,
其他情况返回一个字符串。
如果没有找到匹配或者发生了错误,当subject是数组
时返回一个空数组,其他情况返回null。
范例
示例 #1 比较preg_filter() 和preg_replace()的示例
<?php
$subject = array('1', 'a', '2', 'b', '3', 'A', 'B', '4');
$pattern = array('/\d/', '/[a-z]/', '/[1a]/');
$replace = array('A:$0', 'B:$0', 'C:$0');
echo "preg_filter returns\n";
print_r(preg_filter($pattern, $replace, $subject));
echo "preg_replace returns\n";
print_r(preg_replace($pattern, $replace, $subject));
?>
以上例程会输出:
preg_filter returns
Array
(
[0] => A:C:1
[1] => B:C:a
[2] => A:2
[3] => B:b
[4] => A:3
[7] => A:4
)
preg_replace returns
Array
(
[0] => A:C:1
[1] => B:C:a
[2] => A:2
[3] => B:b
[4] => A:3
[5] => A
[6] => B
[7] => A:4
)
参见
- PCRE 模式
- preg_quote() - 转义正则表达式字符
- preg_replace() - 执行一个正则表达式的搜索和替换
- preg_replace_callback() - 执行一个正则表达式搜索并且使用一个回调进行替换
- preg_grep() - 返回匹配模式的数组条目
- preg_last_error() - 返回最后一个PCRE正则执行产生的错误代码
- PHP(0)
- PHP杂项(34)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- preg_filter 执行一个正则表达式搜索和替换(0)
- preg_grep 返回匹配模式的数组条目(0)
- preg_last_error_msg Returns the error message of the last PCRE regex execution(0)
- preg_last_error 返回最后一个PCRE正则执行产生的错误代码(0)
- preg_match_all 执行一个全局正则表达式匹配(0)
- preg_match 执行匹配正则表达式(0)
- preg_quote 转义正则表达式字符(0)
- preg_replace_callback_array Perform a regular expression search and replace using callbacks(0)
- preg_replace_callback 执行一个正则表达式搜索并且使用一个回调进行替换(0)
- preg_replace 执行一个正则表达式的搜索和替换(0)
- preg_split 通过一个正则表达式分隔字符串(0)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)
宁公网安备 64010402001209号