将特定的eregi_replace转换为preg_replace函数

时间:2013-10-31 22:35:19

标签: php regex

我知道这不是一个新问题,但我不是preg_replace函数的专家。我曾经在互联网上发现这个代码将URL等转换成可点击的链接,但是自从PHP 5.3我得到了一个“已弃用”的通知而且不知道如何修复它...我尝试添加/分隔符,但没有成功。有什么想法吗?

function clickLinks($sText) {
 $sText = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)',
 '<a href="\1" target="_blank"><font color="black">\1</font></a>', $sText);
 $sText = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)',
 '\1<a href="http://\2" target="_blank"><font color="black">\2</font></a>', $sText);
 $sText = eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})',
 '<a href="mailto:\1"><font color="black">\1</font></a>', $sText);
return $sText;
}

2 个答案:

答案 0 :(得分:0)

但要提供帮助。只需在模式的开头和结尾添加分隔符,我使用#并使用\来转义模式中的任何分隔符,因此引擎知道它不是分隔符,所以\#:

$sText = preg_replace('#(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~\#?&//=]+)#',
 '<a href="\1" target="_blank"><font color="black">\1</font></a>', $sText);

答案 1 :(得分:0)

eregi和preg之间的主要区别是preg是基于perl的正则表达式,unix的eregi正则表达式和preg_ *函数必须以删除符开头,如'/'

$regex  = '/[a-z]/'; 
preg_replace($regex, $replacement, $string)

preg_replace