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


php preg_match character special characters ([ ] ( ) { } etc)

我是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 ) ) 

上面的语句不匹配,并返回真实的"["answers"]"如何克服这一点?

只需使用反斜杠' 对其进行转义

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

UPD:

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

preg_match_all('/'[Hello']/i', $word, $match);