如何在与preg_replace_callback的回调相同的类中调用函数?

时间:2012-03-26 10:42:16

标签: php

我有以下功能:

 function make_clickable($ret) {

       $ret = ' ' . $ret;
        // in testing, using arrays here was found to be faster
            $save = @ini_set('pcre.recursion_limit', 10000);
            $retval = preg_replace_callback('#(?<!=[\'"])(?<=[*\')+.,;:!&$\s>])(\()?([\w]+?://(?:[\w\\x80-\\xff\#%~/?@\[\]-]{1,2000}|[\'*(+.,;:!=&$](?![\b\)]|(\))?([\s]|$))|(?(1)\)(?![\s<.,;:]|$)|\)))+)#is', '_make_url_clickable_cb', $ret);
            if (null !== $retval )
                    $ret = $retval;
        @ini_set('pcre.recursion_limit', $save);
        $ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret);
            $ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);
            // this one is not in an array because we need it to run last, for cleanup of accidental links within links
        $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
        $ret = trim($ret);
            return $ret;
    }

以上函数调用三个回调函数,使用preg_replace_callback返回格式化字符串。

function _make_url_clickable_cb($matches) {
                $url = $matches[2];
                $suffix = '';

                /** Include parentheses in the URL only if paired **/
                while ( substr_count( $url, '(' ) < substr_count( $url, ')' ) ) {
                        $suffix = strrchr( $url, ')' ) . $suffix;
                        $url = substr( $url, 0, strrpos( $url, ')' ) );
                }

            $url = esc_url($url);
            if ( empty($url) )
                    return $matches[0];

                $test = parse_url($url);
                if ($test['host'] == 'www.flickr.com' || $test['host'] == 'www.youtube.com') {
                 return $matches[1] . "<a href=\"$url\" rel=\"nofollow\" class='oembed' target='_blank'>$url</a>" . $suffix;
                }
                else {
                 return $matches[1] . "<a href=\"$url\" rel=\"nofollow\" target='_blank'>$url</a>" . $suffix;
                }


        }



function _make_web_ftp_clickable_cb($matches) {
                                    $ret = '';
                                    $dest = $matches[2];
                                    $dest = 'http://' . $dest;
                                    $dest = esc_url($dest);
                                    if ( empty($dest) )
                                        return $matches[0];

                                    // removed trailing [.,;:)] from URL
                                    if ( in_array( substr($dest, -1), array('.', ',', ';', ':', ')') ) === true ) {
                                        $ret = substr($dest, -1);
                                        $dest = substr($dest, 0, strlen($dest)-1);
                                    }
                                    $test = parse_url($dest);
                                        if ($test['host'] == 'www.flickr.com' || $test['host'] == 'www.youtube.com') {
                                            return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\" class='oembed' target='_blank'>$dest</a>" . $suffix;
                                        }
                                        else {
                                            return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\" target='_blank'>$dest</a>" . $suffix;
                                        }
                }

function _make_email_clickable_cb($matches) {
                $email = $matches[2] . '@' . $matches[3];
                return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
        }

这一切都很完美。但是,如果将此函数放入类中,并将main函数(make_clickable)设置为公共函数,并将三个回调设置为private。它不起作用。

我如何在类中调用sunge我为preg_replace_callback指定的回调?

1 个答案:

答案 0 :(得分:1)

尝试将其作为函数名称:

array($this, "_mke_web_ftp_clickable_cb")