php preg_match字符特殊字符([](){}等)

时间:2013-07-05 07:15:43

标签: php preg-match preg-match-all

我是preg_match的新手,我知道这个字符[]在preg_match中有意义,但我如何将其视为我真正想要匹配的字符呢?

例如:

$word = '[Hello], Im steve';
preg_match_all('/[Hello]/', $word, $match);
print_r($match) 

输出:

Array ( [0] => Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => e [6] => e ) ) 

以上陈述没有匹配并返回真实的'['和']' 如何克服这个?

1 个答案:

答案 0 :(得分:1)

使用反斜杠\

进行转义
preg_match_all('/\[Hello\]/', $word, $match); 

print_r($match);

<强> UPD:

不区分大小写的匹配:(分隔符后的i修饰符)

preg_match_all('/\[Hello\]/i', $word, $match); 
相关问题