卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

PHP 7中的preg_replace_callback_array()函数

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中检查文件是否为视频类型?
下一篇: 如何在PHP中增强注册验证功能,防止被刷注册
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏