I need to convert preg_replace() to preg_replace_callback() in this function of an outdated CMS extension:
// 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;
}
I can only imagine what this function does. I tried this but it didsn't work:
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;
}
Could somebody help me on this?