如何删除特殊字符,如;lt;gt但不是锚定标记例如
&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>
这里有一个简单的例子:
<?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;
,然后是任何字符.
,任何次数的*
,直到它匹配字符串?
的下一部分,即&gt;
。
任何时候,只要它发现了这一点,它就会什么都不做。所以你只剩下你想要的文字了。
这是一个工作演示:
http://ideone.com/uSnY0b
使用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
?>