2011-06-02 63 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