Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Preg替换回调验证_Php_Regex_Preg Replace - Fatal编程技术网

Php Preg替换回调验证

Php Preg替换回调验证,php,regex,preg-replace,Php,Regex,Preg Replace,所以我需要重新编写一些在库中找到的旧代码 $text = preg_replace("/(<\/?)(\w+)([^>]*>)/e", "'\\1'.strtolower('\\2').'\\3'", $text); $text = preg_replace("/<br[ \/]*>\s*/","\n",$text); $text = preg_replace("/(^[\r\n]*|[\r

所以我需要重新编写一些在库中找到的旧代码

    $text = preg_replace("/(<\/?)(\w+)([^>]*>)/e",
                         "'\\1'.strtolower('\\2').'\\3'", $text);

    $text = preg_replace("/<br[ \/]*>\s*/","\n",$text);
    $text = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n",
                         $text);

你们能帮我澄清一下我的代码是否正确吗?

这个
$subs
是一个数组,它在第一项中包含整个值,在随后的项中包含捕获的文本。因此,组1在
$subs[1]
中,组2的值在
$subs[2]
中,等等。
$subs[0]
包含整个匹配值,您对其应用了
strtolower
,但原始代码保留了组3的值(使用
([^>]*>)捕获,该值也可能包含大写字母)

使用

$text=preg\u replace\u回调(“~(]*>)~”,函数($subs){
返回$subs[1]。strtolower($subs[2])。$subs[3];
}美元文本);

 $text = preg_replace_callback(
   "/(<\/?)(\w+)([^>]*>)/",
   function($subs) {
       return strtolower($subs[0]);
   },
   $text);
<B>FOO</B>
$text = preg_replace_callback("~(</?)(\w+)([^>]*>)~", function($subs) {
    return $subs[1] . strtolower($subs[2]) . $subs[3]; 
 }, $text);