PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

PHP 7中的preg_replace_callback_array()函数

PHPz
PHPz 转载
2023-08-19 13:42:01 1106浏览

PHP 7中的preg_replace_callback_array()函数

preg_replace_callback_array(patterns, input, limit, count)

参数值:

  • pattern −它需要一个关联数组,将正则表达式模式与回调函数关联起来。
  • input/subject −它需要一个字符串数组来执行替换。
  • limit −它是可选的。默认情况下使用-1,表示无限制。它设置了每个字符串中可以进行多少次替换的限制。
  • count −它也是可选的,就像limit一样。这个变量将包含一个数字,表示函数执行后进行了多少次替换。
  • flags −它可以是preg_offset_capture和preg_unmatched_as_null标志的组合,这些标志影响匹配数组的格式。
  • 返回值 −preg_replace_callback_array()返回一个字符串或字符串数组。如果发现错误,则返回null值。如果找到匹配项,则返回新的subject,否则返回未更改的subject。

Preg_replace_callback_array():示例

演示

<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

以上就是PHP 7中的preg_replace_callback_array()函数的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除