preg_replace_callback_array Perform a regular expression search and replace using callbacks
发表日期:2021-07-01 08:56:56 | 来源: | | 浏览(1172) 分类:PCRE 函数
preg_replace_callback_array
(PHP 7, PHP 8)
preg_replace_callback_array — Perform a regular expression search and replace using callbacks
说明
array
$pattern,string|array
$subject,int
$limit = -1,int
&$count = null,int
$flags = 0): string|array|null
The behavior of this function is similar to preg_replace_callback(), except that callbacks are executed on a per-pattern basis.
参数
-
pattern -
An associative array mapping patterns (keys) to callables (values).
-
subject -
The string or an array with strings to search and replace.
-
limit -
The maximum possible replacements for each pattern in each
subjectstring. Defaults to-1(no limit). -
count -
If specified, this variable will be filled with the number of replacements done.
-
flags -
flagscan be a combination of thePREG_OFFSET_CAPTUREandPREG_UNMATCHED_AS_NULLflags, which influence the format of the matches array. See the description in preg_match() for more details.
返回值
preg_replace_callback_array() returns an array if the
subject parameter is an array, or a string
otherwise. On errors the return value is null
If matches are found, the new subject will be returned, otherwise
subject will be returned unchanged.
更新日志
| 版本 | 说明 |
|---|---|
| 7.4.0 |
The flags parameter was added.
|
范例
示例 #1 preg_replace_callback_array() example
<?php
$subject = 'Aaaaaa Bbb';
preg_replace_callback_array( [ '~[a]+~i' => function ($match) {
echo strlen($match[0]), ' matches for "a" found', PHP_EOL;
}
, '~[b]+~i' => function ($match) {
echo strlen($match[0]), ' matches for "b" found', PHP_EOL;
}
], $subject);
?>
以上例程会输出:
6 matches for "a" found 3 matches for "b" found
参见
- PCRE Patterns
- preg_replace_callback() - 执行一个正则表达式搜索并且使用一个回调进行替换
- preg_quote() - 转义正则表达式字符
- preg_replace() - 执行一个正则表达式的搜索和替换
- preg_last_error() - 返回最后一个PCRE正则执行产生的错误代码
- Anonymous functions
- 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号