PHP 用链接替换URL - PHP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 用链接替换URL - PHP相关的知识,希望对你有一定的参考价值。

function replace_links( $text ) 
{	
	$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);

	$ret = ' ' . $text;
	
	// Replace Links with http://
	$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\" rel=\"nofollow\">\\2</a>", $ret);
	
	// Replace Links without http://
	$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\" rel=\"nofollow\">\\2</a>", $ret);

	// Replace Email Addresses
	$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
	$ret = substr($ret, 1);
	
	return $ret;
}

以上是关于PHP 用链接替换URL - PHP的主要内容,如果未能解决你的问题,请参考以下文章