2016-02-16 60 views
0

我在使用preg_replace時遇到了這個錯誤,它與某些html混合使用。正則表達式未知修飾符

警告:的preg_replace():未知的修飾詞 'd'

這裏是我使用的代碼。

它從包含html的字符串的開始處移除該位的html。

$foo = preg_replace("/^<div id='IDHERE'>sometext.</div>/", '', $foo); 

回答

1

你需要躲避/,因爲它是用來結束正則表達式的字符串的第一部分,在這之後,調節劑如g(全球),並將d是不是一個有效的選項有(在d來自div)。

讓它\/

  $foo = preg_replace("/^<div id='IDHERE'>sometext.<\/div>/", '', $foo); 
0

逃生收盤</div>標籤:

$foo = preg_replace("/^<div id='IDHERE'>sometext.<\/div>/", '', $foo); 

PHP是解釋在結束的div標籤正斜槓作爲表達的末尾:

/^<div id='IDHERE'>sometext.</div>/ 
          ^PHP thinks this is the end, 
           then complains about the unknown modifier 'd' 
0

add \ in you r模式:

$foo = preg_replace("/^<div id='IDHERE'>sometext.<\/div>/", '', $foo); 

here參考。

0

你需要</div>逃脫/,所以你的正則表達式應該是:

$foo = preg_replace("/^<div id='IDHERE'>sometext.<\/div>/", '', $foo); 
+0

此外,這是測試一個很好的資源:http://www.phpliveregex.com/ – Jeffwa

相關問題