2016-01-13 54 views
-1

如何刪除特殊字符等; LT; GT但不錨標籤 如刪除特殊字符,如lt;但不錨標記

&amp;lt;a href=&amp;quot;http://www.imdb.com/name/nm0005069/&amp;quot;&amp;gt;Spike Jonze&amp;lt;/a&amp;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

讓我們瞭解您嘗試請。 – HJerem

+0

我想刪除lt; GT; like標籤但沒有刪除字符串 – areeb

回答

1

這裏有一個快速爲你:

<?php 

// SET OUR DEFAULT STRING 
$string = '&amp;lt;a href=&amp;quot;http://w...content-available-to-author-only...b.com/name/nm0005069/&amp;quot;&amp;gt;Spike Jonze&amp;lt;/a&amp;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('~&amp;lt;.*?&amp;gt;~', '', $string); 

// PRINT OUT OUR NEW STRING 
print $string; 

所有我在這裏做的是尋找&amp;lt;,後跟任何字符.,任意次數*,直到它匹配字符串?的下一部分,即&amp;gt;

任何時候它發現它,它沒有任何代替。所以你留下了你想要的文字。

這裏是一個工作演示:

http://ideone.com/uSnY0b

0

使用html_entity_decode:

<?php $url = html_entity_decode('&amp;lt;a href=&amp;quot;http://www.imdb.com/name/nm0005069/&amp;quot;&amp;gt;Spike Jonze&amp;lt;/a&amp;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 
?> 
+0

但我想刪除所有文本之間的lt; a但未刪除的anker標籤輸出應該是「Spike Jonze」 – areeb

+0

strip_tags從字符串中刪除所有標籤,但不想刪除具有已經在RobHallums' – areeb