2011-06-02 61 views
0

代碼:需要幫助從鏈接中刪除HTML標籤

$message = "<a href=\"http://www.stackoverflow.com\" target=\"new\">My link</a>"; 

function unclick ($message) { 
$message = preg_replace("#\<a.+href\=[\"|\'](.+)[\"|\'].*\>.*\<\/a\>#U","$1",$message); 
$message = str_replace("mailto:","",$message); 
return $message; 
} 

當鏈接看起來像這樣這隻適用:

http://stackoverflow.com/ 

不幸的是,它不適合長期環節的工作:

http://stackoverflow.com/questions/ask 

需要一種方法來從所有鏈接中刪除html標籤。謝謝。

+0

爲我工作:) – Tapos 2011-06-02 16:42:02

+0

是您希望輸出'「我的鏈接」'或'的「http://計算器.com/questions/ask「'? – jball 2011-06-02 16:49:17

+0

這就是['strip_tags()'](http://php.net/strip_tags)的用途。 – ceejayoz 2011-06-02 16:34:51

回答

0

我測試了你的代碼,它對我來說工作正常。

另一種解決方案:

必須在此在PHP來實現,也可以使用JavaScript與jQuery?

根據您的標題:

yourlink.removeAttr('href'); 

根據你的代碼做什麼:

var url = yourlink.attr('href'); 
0
<pre> 
<?php 
$input = '<a href="http://www.stackoverflow.com/hello/hi/bye">hello</a>'; 
$sx = simplexml_load_string($input); 
echo ($sx['href']); 
?> 
</pre> 

試試這個。它正常工作,如果網址不escaped with \ 如果逃跑,然後使用這個腳本:

<pre> 
<?php 
$input = '<a href=\"http://www.stackoverflow.com/hello/hi/bye\">hello</a>'; 
$input = str_replace('\\','',$input); 
$sx = simplexml_load_string($input); 
echo ($sx['href']); 
?> 
</pre> 
+0

$輸入可能不僅包含1個鏈接。它可能包含文本和許多鏈接。我需要從所有鏈接中刪除html標籤,只留下普通的URL。 – rlh 2011-06-02 19:53:26

+0

然後請用一些輸入更新問題。 – 2011-06-03 03:19:56