Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 10 in

一、总结

1、[是正则中特殊字符,需要用\反斜杠转义

二、Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 10 in

1、问题

I've got a series of .txt files that I'm reading in a for loop. And I've placed a token in some of the text files in the form [widget_]

So, the entire contents of the text file might be [widget_search] for example. And another text file might contain the content [widget_recent-posts]. Others might just have html formatted text and not have the token at all.

In the for loop, I'm doing a preg_match to see if the text file is one in which the contents matches my token pattern. And if a match, I'm executing some conditional code.

However, I'm getting an error when I run a trace test to see if there's a match.

The error is:

Warning: preg_match() [function.preg-match]: Compilation failed: missing terminating ] for character class at offset 8 in C:\xampplite\htdocs\test\wp-content\plugins\widget-test\widget-test.php on line 227

And here's the code at line 227:

if (preg_match("/[widget_/i",$widget_text)) {//do something}

2、解答

You should escape the [ character like this:

if (preg_match("/\[widget_/i",$widget_text)) {//do something}

3、相似错误

$start="/^<![CDATA[/";
$v=preg_replace($start,"",$v);

三、测试题-简答题

1、正则中常见的需要转义的特殊字符?

解答:\. \/   \[  \(  \(  \d

最新文章

  1. C++_系列自学课程_第_11_课_类型转换_《C++ Primer 第四版》
  2. iOS开发~CocoaPods使用详细说明
  3. 简单区分VMware的三种网络连接模式(bridged、NAT、host-only)
  4. cdoj 1328 卿学姐与诡异村庄 Label:并查集 || 二分图染色
  5. atitit.无线网卡 不能搜索到WiFi 无线路由信号的解决不能上网
  6. 关于Fragment 不响应onActivityResult的情况分析 (
  7. python操作sqlite数据库
  8. 有关loading share object file libjvm.so: xxxxx 的那些问题
  9. 重构5-Pull Up Field(字段上移)
  10. We Recommend a Singular Value Decomposition
  11. 50道经典的JAVA编程题 (1-5)
  12. Can you find it?(二分 二分+STL set map)
  13. jquery 中时间的运用
  14. VirtualBox的共享文件夹功能的使用演示
  15. WPF Tutorial - Using A Visual Collection
  16. bzoj4271: chemistry 化学
  17. vue基础——事件处理
  18. 集合(三)CopyOnWriteArrayList
  19. LoadRunner如何监控Linux系统资源
  20. 24.Java中atomic包中的原子操作类总结

热门文章

  1. Vue 打包后报错 Uncaught TypeError: Cannot redefine property: $router
  2. Office2010激活工具
  3. echarts如何设置背景图的颜色
  4. STL中erase()的陷阱
  5. SFC梯形图编程
  6. Spring.net的Demo项目,了解什么是控制反转
  7. [Javascript AST] 1. Continue: Write a simple Babel plugin
  8. Windows环境下ARM集成开发环境的搭建与使用
  9. [bzoj1269]文本编辑器editor [bzoj1500]维修数列
  10. 文字记录而已!!人民币直充/兑换PayPal美金