您好,欢迎访问一九零五行业门户网

PHP 7中的preg_replace_callback_array()函数

preg_replace_callback_array()函数在php 7中表示一个正则表达式,并替换了回调函数的使用。此函数返回一个字符串或字符串数组,以匹配一组正则表达式,并使用回调函数进行替换。
syntaxpreg_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" found4 number of "b" found5 number of "c" found
以上就是php 7中的preg_replace_callback_array()函数的详细内容。
其它类似信息

推荐信息