2017-04-20 103 views
4

紅粉這樣形成一個字符串中刪除多個子如何從兩個標籤

hello Rose and John. 

我怎麼能呢?

+0

你已經試過了什麼? – BenM

+0

我在這裏找到函數稱爲:delete_all_between,但它只適用於第一次發生,但不適用於所有。 –

+0

[PHP:從HTML字符串剝離特定標記?]可能重複(http://stackoverflow.com/questions/3308530/php-strip-a-specific-tag-from-html-string) – BenM

回答

6

我想你可以使用:

$new_html = preg_replace('%<tag>.*?</tag> ?%si', '', $old_html); 
+0

工作!但是有一個錯誤。當字符串以結尾不起作用;如何修復它? –

+0

只需使用'?'製作空間'可選',我就可以更新答案。 –

0

如果你不想使用DOM,你總是可以使用preg_replace做到這一點。

$content = 'hello Rose <tag> Micheal </tag> and <tag> July </tag> John.'; 

$content = preg_replace('/<tag>[^<]+<\/tag>/i', '', $content); 

print $content; // returns: hello Rose and John.