www.google.com
或http://www.google.com
auto_link()
功能將添加鏈接是否正確
..
但是,如果鍵入
google.com
的功能不起作用,因爲www。部分丟失...
我怎樣才能確保它將包括這些鏈接?
這是笨的功能:
/**
* Auto-linker
*
* Automatically links URL and Email addresses.
* Note: There's a bit of extra code here to deal with
* URLs or emails that end in a period. We'll strip these
* off and add them after the link.
*
* @access public
* @param string the string
* @param string the type: email, url, or both
* @param bool whether to create pop-up links
* @return string
*/
if (! function_exists('auto_link'))
{
function auto_link($str, $type = 'both', $popup = FALSE)
{
if ($type != 'email')
{
if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches))
{
$pop = ($popup == TRUE) ? " target=\"_blank\" " : "";
for ($i = 0; $i < count($matches['0']); $i++)
{
$period = '';
if (preg_match("|\.$|", $matches['6'][$i]))
{
$period = '.';
$matches['6'][$i] = substr($matches['6'][$i], 0, -1);
}
$str = str_replace($matches['0'][$i],
$matches['1'][$i].'<a href="http'.
$matches['4'][$i].'://'.
$matches['5'][$i].
$matches['6'][$i].'"'.$pop.'>http'.
$matches['4'][$i].'://'.
$matches['5'][$i].
$matches['6'][$i].'</a>'.
$period, $str);
}
}
}
if ($type != 'url')
{
if (preg_match_all("/([a-zA-Z0-9_\.\-\+]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i", $str, $matches))
{
for ($i = 0; $i < count($matches['0']); $i++)
{
$period = '';
if (preg_match("|\.$|", $matches['3'][$i]))
{
$period = '.';
$matches['3'][$i] = substr($matches['3'][$i], 0, -1);
}
$str = str_replace($matches['0'][$i], safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period, $str);
}
}
}
return $str;
}
}
SOLUTION: 功能auto_link($海峽,$型= '都',$彈出= FALSE) { 如果($型=! ''email') {!preg_match_all(「/ ^([a-zA-Z0-9 _.-])+ @([a-zA-Z0-9 _.-])+ \。([a- zA-Z])+([a-zA-Z])+ /「,$ str,$ matches)){
if (preg_match_all("/(?:https?\:?(?:\/\/)?|www\.)?([a-zA-Z0-9\-\.]+\.(?:.[a-z]*))/mi", $str, $matches))
{
$pop = ($popup == TRUE) ? " target=\"_blank\" " : "";
for ($i = 0; $i < count($matches['0']); $i++)
{
$str = str_replace($matches[0][$i],
'<a href="http://'.$matches[1][0].'" class="auto_link_color">'.$matches[1][0].'</a>', $str);
}
}
}else{
for ($i = 0; $i < count($matches['0']); $i++)
{
$str = str_replace($matches[0][$i], $matches[0][0], $str);
}
}
}
return $str;
}
這解決了我的問題,所以用戶輸入的任何鏈接都會找到它並添加鏈接...即使用戶輸入電子郵件,也不會在域部分添加鏈接,而是將其顯示爲文本。
你的邏輯有瑕疵,試圖強制'auto_link()'來做到這一點。它會爲''創建一個亂七八糟的',你會最終得到一些不應該包含的文本的隨機URL。即使如此,在這裏並沒有將所有網址格式化爲真正的鏈接那麼雄心勃勃。想想看。你會爭取一些東西是電子郵件或網址,或者只是一段時間的刺痛。只是不要混淆至少恕我直言,沒有被打破的東西。 – Jakub 2012-02-23 03:13:55