如何删除特殊字符,例如:lt; gt但不删除Anchor标记 e.g
&lt;a href=&quot;http://www.imdb.com/name/nm0005069/&quot;&gt;Spike Jonze&lt;/a&gt; This cause by <a class="primary-black" href="http://example.com/community/RobHallums">RobHallums</a>
应该是
Spike Jonze This cause by <a class="primary-black" href="http://example.com/community/RobHallums">RobHallums</a>
答案 0 :(得分:1)
这是一个快速的给你:
<?php
// SET OUR DEFAULT STRING
$string = '&lt;a href=&quot;http://w...content-available-to-author-only...b.com/name/nm0005069/&quot;&gt;Spike Jonze&lt;/a&gt; This cause by <a class="primary-black" href="http://e...content-available-to-author-only...e.com/community/RobHallums">RobHallums</a>';
// USE PREG_REPLACE TO STRIP OUT THE STUFF WE DON'T WANT
$string = preg_replace('~&lt;.*?&gt;~', '', $string);
// PRINT OUT OUR NEW STRING
print $string;
我在这里所做的就是寻找&lt;
,后跟任何字符.
,任意次*
,直到匹配字符串的下一部分{{1} },这是?
。
任何时候它发现,它都没有替换它。所以你留下了你想要的文字。
这是一个有效的演示:
答案 1 :(得分:0)
使用html_entity_decode:
<?php $url = html_entity_decode('&lt;a href=&quot;http://www.imdb.com/name/nm0005069/&quot;&gt;Spike Jonze&lt;/a&gt;');
echo $url;
?>
输出将是:
<a href="http://www.imdb.com/name/nm0005069/">Spike Jonze</a>
编辑:
<?php
preg_match_all('/<a .*?>(.*?)<\/a>/',$url,$matches);
//For Text Name
echo $matches[1][0]; //output : Spike Jonze
?>