2011-03-05 136 views
1

我使用此代码来制作纯文本超链接。从纯文本创建超链接

$text = eregi_replace('(((f|ht){1}tp://)[[email protected]:%_\+.~#?&//=]+)', 
    '<a target="_blank" href="\\1">\\1</a>', $text); 
     $text = eregi_replace('(((f|ht){1}tps://)[[email protected]:%_\+.~#?&//=]+)', 
    '<a target="_blank" href="\\1">\\1</a>', $text); 
    $text = eregi_replace('([[:space:]()[{}])(www.[[email protected]:%_\+.~#?&//=]+)', 
    '\\1<a target="_blank" href="http://\\2">\\2</a>', $text); 
    $text = eregi_replace('([_\.0-9a-z-][email protected]([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', 
    '<a target="_blank" href="mailto:\\1">\\1</a>', $text); 

但它不工作的时候文本www.domain.com或domain.com或subdomain.domain.com

我怎样才能使它与他们合作?

感谢

+0

可能的重复[如何链接到一个字符串与PHP的url?](http://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with- php) – 2011-03-05 15:30:39

回答

1

有这个问题,这是张女士,使得它更难以找到合适的人的复制品堆。接受这个列表,而不是:

但既然你已经有了,你觉得适合你的东西,会有选项只添加边缘情况。寻找sub.domain.com不容易,可能会导致误报。但是,将它们转换为http://网址,以便其他规则接他们将有可能通过应用此第一:

$text = preg_replace('#(?<!://)w+\.\w+\.(com|net|org|\w\w)\b\S*#', "http://$0", $text); 
$text = preg_replace('#(?<!://)www\.\S+#', "http://$0", $text); 

而不是\S你可以使用你的冗长字符类。