Preg_replace_callback_array()PHP 7中的function代表一个正则表达式,并取代了回调的使用。此函数返回一个字符串或字符串数组以匹配一组正则表达式,并使用回调函数替换它们。
preg_replace_callback_array(patterns, input, limit, count)
pattern-它需要一个关联数组,以将正则表达式模式关联到回调函数。
输入/主题-它需要一个字符串数组来执行替换。
限制-这是可选的。-1为默认值,表示它是无限的。它为每个字符串中可以进行多少次替换设置了限制。
count-它也是可选的,例如限制。该变量将包含一个数字,指示执行该功能后执行了多少次替换。
标志-它可以是preg_offset_capture和preg_unmatched_as_null标志的组合,这会影响匹配数组的格式。
返回值-preg_replace_callback_array()返回一个字符串或strings.If找到一个错误数组,然后将返回value.If找到的null匹配项,将返回新主题,否则,主题将保持不变。
<html>
<head>
<title> PHP 7 Featuretutorialpoint:</title>
</head>
<body>
<?php
$subject = 'AaaaaaaBbbbCccc';
preg_replace_callback_array (
[
'~[a]+~i' => function ($match) {
echo strlen($match[0]), ' number of "a" found', PHP_EOL;
},
'~[b]+~i' => function ($match) {
echo strlen($match[0]), ' number of "b" found', PHP_EOL;
},
'~[c]+~i' => function ($match) {
echo strlen($match[0]), ' number of "c" found', PHP_EOL;
}
],
$subject
);
?>
</body>
</html>
输出结果上面的程序代码的输出是-
7 number of "a" found
4 number of "b" found
5 number of "c" found