2012-05-24 79 views
0

我用這個從字體標籤乾淨的HTML:PHP的正則表達式排除

$html = '<font class="textmiddle" color="red">toto</font>' ; 
$clean = preg_replace('/<font[^>]*>/', '', $html) ; 
$clean = preg_replace('/<\/font>/', '', $clean) ; 

它的工作原理就像一個魅力。

但是,當HTML字符串是:

$html = '<font class="textmiddle" color="<%= color.importanttext %>">toto</font>' ; 

那麼結果是沒有預期的一個:

">toto 
+1

這是[此鏈接]的主要候選人(http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)(有人必須) - 但非常嚴肅的是,你爲什麼要在PHP中處理原始的ASP代碼? – DaveRandom

+0

總之,雖然你想要用正則表達式來完成,但不要這樣做。請使用其中一個可用的xml/html解析器編寫可維護的健壯代碼。檢查這也是:http://stackoverflow.com/questions/188414/best-xml-parser-for-php – FailedDev

+2

@FailedDev相當http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662因爲這不是XML。無論如何,這可能是重複的。 – Gordon

回答

1

嘗試

<?php 
    $html = '<font class="textmiddle" color="<%= color.importanttext %>">toto</font>' ; 
    $clean = preg_replace('/<font\s.*">/SimU', '', $html) ; 
    echo $clean; 
?> 

但是請注意,你

toto</font> 

輸出。

+0

謝謝!它幫助我完美 –