2011-07-14 66 views
0

我需要讓我的用戶意識到他們正在點擊外部鏈接。我在我的網站上有很多種聊天方式,有時用戶會發布鏈接,這些鏈接對他們來說可能很危險,所以我想在離開網站之前提醒他們。重定向用戶之前捕獲外部鏈接

例如eveonline.com使用在他們的論壇以下內容:http://www.eveonline.com/externalLink.aspx?l=http://altdevblogaday.com/2011/07/11/the-hidden-evil-of-the-micro-transaction/

每當一個鏈接顯示了他們認爲如果域比eveonline.com不同,如果這是他們加入的「http:// WWW .eveonline.com/externalLink.aspx?l =「。

這是我的makeClickableLinks函數,我用它來使鏈接可點擊,我想知道如果有人可以重寫,如果我做了上述+可點擊,因爲我沒有寫這個函數,因爲我對preg_match無能爲力。

function makeClickableLinks($text) 
    { 
    $text = eregi_replace('(((f|ht){1}tp://)[[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); 


    return $text; 
    } 

變量$ text是用戶的帖子。

+0

學習正則表達式,然後preg_match =容易peasy。從這裏開始:http://gskinner.com/RegExr/ – kevtrout

回答

1

我不是PHP的嚮導,但試試這個。請注意,幾乎所有這些都是直接從PHP手冊here中獲取的。

function makeClickableLinks($text) 
{ 
    $text = eregi_replace('(((f|ht){1}tp://)[[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); 

    $regex = '@(<a.*?href=")((?!(?:(?:f|ht)tps?://)?(?:[a-z0-9]+\.)?domain\.com)[^"]*)@i'; 
    $replacement = '$1http://your.domain.com/externLink.php?l=$2'; 

    $text = preg_replace($regex, $replacement, $text); 

    return $text; 
} 
+0

感謝elmugrat,我想你錯過了$ text是用戶的帖子,它帶有文本,一些
可能和URLS沒有「a」標籤的事實。這就是我的功能,返回相同的文本,但添加了「a」標籤到URLS。我試圖讓這個函數看看這個鏈接是否是外部的,如果是這樣的話,請添加我的重定向器。再次感謝。 –

+0

好的,我確實誤解了這一點。我會編輯我的答案......查看幾個。 –

+0

謝謝隊友,我收到警告警告:eregi_replace()[function.eregi-replace]:REG_BADRPT在第3 eregi_replace行,想知道這是什麼... –

相關問題