2016-02-16 58 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

相关问题