php解析邮件,Php解析链接/电子邮件

我想知道是否有一个简单的代码片段可以转换任何类型的链接:

http://www.cnn.com to http://www.cnn.com

cnn.com to cnn.com

www.cnn.com to www.cnn.com

abc@def.com to to mailto:abc@def.com

我不想使用任何PHP5特定的库.

感谢您的时间.

更新我已将上述文本更新为我想要转换为的内容.请注意,案例2和3的href标签和文本不同.

UPDATE2怎么做gmail聊天呢?他们很聪明,只适用于真实域名.例如a.ly工作,但a.cb不起作用.

解决方法:

/**

NAME : autolink()

VERSION : 1.0

AUTHOR : J de Silva

DESCRIPTION : returns VOID; handles converting

URLs into clickable links off a string.

TYPE : functions

======================================*/

function autolink( &$text, $target='_blank', $nofollow=true )

{

// grab anything that looks like a URL...

$urls = _autolink_find_URLS( $text );

if( !empty($urls) ) // i.e. there were some URLS found in the text

{

array_walk( $urls, '_autolink_create_html_tags', array('target'=>$target, 'nofollow'=>$nofollow) );

$text = strtr( $text, $urls );

}

}

function _autolink_find_URLS( $text )

{

// build the patterns

$scheme = '(http:\/\/|https:\/\/)';

$www = 'www\.';

$ip = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';

$subdomain = '[-a-z0-9_]+\.';

$name = '[a-z][-a-z0-9]+\.';

$tld = '[a-z]+(\.[a-z]{2,2})?';

$the_rest = '\/?[a-z0-9._\/~#&=;%+?-]+[a-z0-9\/#=?]{1,1}';

$pattern = "$scheme?(?(1)($ip|($subdomain)?$name$tld)|($www$name$tld))$the_rest";

$pattern = '/'.$pattern.'/is';

$c = preg_match_all( $pattern, $text, $m );

unset( $text, $scheme, $www, $ip, $subdomain, $name, $tld, $the_rest, $pattern );

if( $c )

{

return( array_flip($m[0]) );

}

return( array() );

}

function _autolink_create_html_tags( &$value, $key, $other=null )

{

$target = $nofollow = null;

if( is_array($other) )

{

$target = ( $other['target'] ? " target=\"$other[target]\"" : null );

// see: http://www.google.com/googleblog/2005/01/preventing-comment-spam.html

$nofollow = ( $other['nofollow'] ? ' rel="nofollow"' : null );

}

$value = "$key";

}

?>

标签:php,regex

来源: https://codeday.me/bug/20190522/1151836.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值