我有一个下面的函数:
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_clicable)被设置为公共函数,三个回调被设置为私有。它不起作用。
我该如何在类中调用我为preg_replace_callback指定的回调?
尝试将其作为函数名:
array($this, "_mke_web_ftp_clickable_cb")