我需要在过时的CMS扩展程序的此函数中将preg_replace()转换为preg_replace_callback():
// santizes a regex pattern
private static function sanitize( $pattern, $m = false, $e = false ) {
if( preg_match( '/^\/(.*)([^\\\\])\/(.*?)$/', $pattern, $matches ) ) {
$pat = preg_replace(
'/([^\\\\])?\(\?(.*\:)?(.*)\)/Ue',
'\'$1(?\' . self::cleanupInternal(\'$2\') . \'$3)\'',
$matches[1] . $matches[2]
);
$ret = '/' . $pat . '/';
if( $m ) {
$mod = '';
foreach( self::$modifiers as $val ) {
if( strpos( $matches[3], $val ) !== false ) {
$mod .= $val;
}
}
if( !$e ) {
$mod = str_replace( 'e', '', $mod );
}
$ret .= $mod;
}
} else {
$pat = preg_replace(
'/([^\\\\])?\(\?(.*\:)?(.*)\)/Ue',
'\'$1(?\' . self::cleanupInternal(\'$2\') . \'$3)\'',
$pattern
);
$pat = preg_replace( '!([^\\\\])/!', '$1\\/', $pat );
$ret = '/' . $pat . '/';
}
return $ret;
}
我只能想象这个功能的作用。我尝试了这个,但它没有工作:
private static function sanitize( $pattern, $m = false, $e = false ) {
if( preg_match( '/^\/(.*)([^\\\\])\/(.*?)$/', $pattern, $matches ) ) {
$pat = preg_replace_callback(
'/([^\\\\])?\(\?(.*\:)?(.*)\)/U',
function($matches) {return CallFunction('\'$1(?\' . self::cleanupInternal(\'$2\') . \'$3)\''); },
$matches[1] . $matches[2]
);
$ret = '/' . $pat . '/';
if( $m ) {
$mod = '';
foreach( self::$modifiers as $val ) {
if( strpos( $matches[3], $val ) !== false ) {
$mod .= $val;
}
}
if( !$e ) {
$mod = str_replace( 'e', '', $mod );
}
$ret .= $mod;
}
} else {
$pat = preg_replace_callback(
'/([^\\\\])?\(\?(.*\:)?(.*)\)/U',
function($matches) {return CallFunction('\'$1(?\' . self::cleanupInternal(\'$2\') . \'$3)\''); },
$pattern
);
$pat = preg_replace( '!([^\\\\])/!', '$1\\/', $pat );
$ret = '/' . $pat . '/';
}
return $ret;
}
有人可以帮我吗?
答案 0 :(得分:1)
没有任何确定,您可以尝试第一个preg_replace,并以相同的方式修改第二个preg_replace:
$that = $this;
$pat = preg_replace_callback(
'/([^\\\\])?\(\?(.*:)?(.*)\)/U',
function ($m) use ($that) {
return $m[1] . '(?' . $that->cleanupInternal($m[2]) . $m[3];
},
$matches[1] . $matches[2]
);
作为旁白评论,我认为([^\\\\])?
没有任何意义或对某些内容有用,因为它是可选的,并且在相同位置的替换字符串中重用。