我有一個函數可以將超鏈接中的字符串轉換爲「www.example.com」和「http://example.com」。它還處理子域例如'http://sub.example.com'。PHP - 在文本中查找鏈接的功能
但它不能與這一個 - http://www .example.com的」輸出以下
<a href="http://<a href="http://www.chemica.ru">www.chemica.ru</a>">http://<a href="http://www.chemica.ru">www.chemica.ru</a></a>
請,誰能幫助?問題是'http://'和'www'都是。在一起,都有不同的轉換方式。
function makeLinks($text){
$text = eregi_replace('(((f|ht){1}tp://)[[email protected]:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $text);
$text = eregi_replace('(www.[[email protected]:%_\+.~#?&//=]+)', '<a href="http://\\1">\\1</a>', $text);
$text = eregi_replace('([_\.0-9a-z-][email protected]([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $text);
return $text;
}
僅供參考,eregi_ *函數已棄用。您應該在不久的將來在某個時刻切換到preg_replace。 – webbiedave 2010-09-21 17:18:40
PCRE還允許比POSIX更高級的正則表達式功能。 – 2010-09-21 18:01:59
可能重複的[如何從段落中提取http鏈接並將它們存儲在php中的數組](http://stackoverflow.com/questions/6861324/how-to-extract-http-links-from-a-paragraph -and-STORE-它們-IN-A-陣列上PHP) – hakre 2012-01-28 20:16:55