php网页自动点击一个链接,【PHP-网页处理】自动转换URL为可点击的超链接。在WordPress中,如果你想在字符串中自动转换所有的URL成可点击的超链接,那么使用内置函数make_clickab...

function _make_url_clickable_cb($matches) {

$ret = '';

$url = $matches[2];

if ( empty($url) )

return $matches[0];

// removed trailing [.,;:] from URL

if ( in_array(substr($url, -1), array('.', ',', ';', ':')) === true ) {

$ret = substr($url, -1);

$url = substr($url, 0, strlen($url)-1);

}

return $matches[1] . "$url" . $ret;

}

function _make_web_ftp_clickable_cb($matches) {

$ret = '';

$dest = $matches[2];

$dest = 'http://' . $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);

}

return $matches[1] . "$dest" . $ret;

}

function _make_email_clickable_cb($matches) {

$email = $matches[2] . '@' . $matches[3];

return $matches[1] . "$email";

}

function make_clickable($ret) {

$ret = ' ' . $ret;

// in testing, using arrays here was found to be faster

$ret = preg_replace_callback('#([\s>])([\w]+?://[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_url_clickable_cb', $ret);

$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("#(]+?>|>))]+?>([^>]+?)#i", "$1$3", $ret);

$ret = trim($ret);

return $ret;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值