【问题标题】:How do I call a function within the same class as a callback of preg_replace_callback?如何调用与 preg_replace_callback 回调相同的类中的函数?
【发布时间】:2012-04-09 20:53:35
【问题描述】:

我在下面有一个函数:

 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>";
        }

这一切都完美无缺。但是,当它放在一个类中时,主函数(make_clickable)设置为公共函数,三个回调设置为私有。它不起作用。

我将如何在一个类中调用我为 preg_replace_callback 指定的回调?

【问题讨论】:

    标签: php


    【解决方案1】:

    试试这个作为函数名:

    array($this, "_mke_web_ftp_clickable_cb")
    

    【讨论】:

    • 感谢 Fender,您的解决方案对我来说很有意义,但它似乎仍然不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 2014-06-17
    • 2016-02-12
    相关资源
    最近更新 更多