Convert a function from preg_replace to preg_replace_callback()

烂漫一生 提交于 2019-12-02 22:25:43

问题


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?


回答1:


Without any certitude, you can try this for the first preg_replace, and modify the second preg_replace in a same way:

$that = $this;
$pat = preg_replace_callback(
            '/([^\\\\])?\(\?(.*:)?(.*)\)/U',
            function ($m) use ($that) {
                return  $m[1] . '(?' . $that->cleanupInternal($m[2]) . $m[3];
            },
            $matches[1] . $matches[2]
);

As an aside comment, I don't think that ([^\\\\])? has any sense or is useful for something since it is optional and reuse in the replacement string at the same position.



来源:https://stackoverflow.com/questions/26370427/convert-a-function-from-preg-replace-to-preg-replace-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!