2014-12-22 97 views
-10

帶標籤,我需要一個a元素中去除所有標籤,例如:PHP:內<a>

<p>Lorem ipsum 
    <span style="color:#cc3399;">dolor sit</span> amet <a href="myurl.html"> 
    <span style="color:#cc3399;">hello</span></a> 
</p> 

必須是:

<p>Lorem ipsum 
    <span style="color:#cc3399;">dolor sit</span> amet 
    <a href="myurl.html">hello</a> 
</p> 
+3

[用strip_tags()](http://php.net/strip_tags)應該有所幫助。 – emmanuel

+0

我需要一個正則表達式,因爲我必須做其他操作 – Daniele

+0

@Daniele不,你不 – PeeHaa

回答

-1
$text = '<a href="myurl.html"> 
<span style="color:#cc3399;">hello</span> 
</a>'; 

echo strip_tags($text, '<a>'); 

這應該工作,它使用函數strip_tags。第一個參數是要剝離標籤的文本,第二個參數是不需要剝離標籤的參數。

在這種情況下,它的標籤。

查看更多about strip_tags here

0

試試這個

$re = "/(<[^\\/>]+\\/?>)*([^<]+)(<\\/\\w+>)*/m"; 
    $str = "<a href=\"myurl.html\">\n<span style=\"color:#cc3399;\">hello</span>\n</a>"; 

    preg_match_all($re, $str, $matches); 
    //var_dump($matches); 
$matches[1]; //for open tag array 
$matches[2]; //for inner data array 
$matches[3]; //for close tag array 
    $md=""; 
    $c=count($matches[1]); 
    foreach($matches[1] as $k=>$v){ 
     if($k==0){ 
      $md.=$v.$matches[2][$k]; 
     } 
     else if ($c==$k+1){ 
      $md.=$matches[2][$k].$matches[3][$k]; 
     } 
     else{ 
      $md.=$matches[2][$k]; 
     } 
    } 
var_dump($md); 

輸出

string '<a href="myurl.html"> 
hello 
</a>' (length=32) 

live demo

+0

... mmmm以及爲什麼來自wiki? – NoobEditor

+0

@Ahosan Karim Asik這不是我想要的,請仔細閱讀我的要求 – Daniele

+0

您是否停止提供所有不合適的答案? – nhahtdh

-1

谷歌的一點點會幫助你。

源:Remove span from a

你要刪除的標籤也的內容是什麼?如果是的話,試試這個:

$string = preg_replace("/<span[^>]+\>/i", "", $string); 

否則這將是蠻好的:

F你只是想刪除一個標籤裏面跨度標籤和您的格式,那麼你貼什麼下列符合這一規範。通過empty string。看到演示

$string = str_replace('<a href="#"> <span>', '<a href="#">', $string); 
$string = str_replace("</span> </a>", "</a>", $string); 
+0

爲什麼downvote? – Matheno

+0

你的解決方案不能解決你的問題,因爲通過這種方式刪除所有的跨度,而不是我想刪除標籤之間的跨度 – Daniele

+0

嗯,這不是我的問題實際上,我發佈了這個片段的實際來源,所以你可以自己查看它並做一些努力。那裏有很多好的建議。 – Matheno