需要帮助从链接中删除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标签。谢谢。
答
当然,我们怎样才能不发布bobince强大的答案:
如果您需要解析HTML,你可能想使用DOMDocument
答
我测试了你的代码,它对我来说工作正常。
另一种解决方案:
必须在此在PHP来实现,也可以使用JavaScript与jQuery?
根据您的标题:
yourlink.removeAttr('href');
根据你的代码做什么:
var url = yourlink.attr('href');
答
<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
为我工作:) – Tapos 2011-06-02 16:42:02
是您希望输出'“我的链接”'或'的“http://计算器.com/questions/ask“'? – jball 2011-06-02 16:49:17
这就是['strip_tags()'](http://php.net/strip_tags)的用途。 – ceejayoz 2011-06-02 16:34:51