[英]Need help to remove html tags from a link
代碼:
$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>
嘗試這個。 如果 url 未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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.