[thelist] PHP - preg_match - brackets [...]

Bob Meetin bobm at dottedi.biz
Tue May 15 01:49:17 CDT 2012

With brackets having a special meaning in PHP, how do you correctly escape an expression that includes [...] as part of the content so that it tests positive without erroring?  The expression, [...] may make up the entire content fed in as $content = "[...]";, the very beginning, the end or somewhere in between.


$content = "apple pie [...] strudel cookies";
if (preg_match("/[...]/i", "$content")) { $pass = TRUE; }

I've tried various things like using the backslash to escape the bracket meaning, single ticks, adding a backslash in front of each period, etc.

if (preg_match("\/[...\]/i", "$content")) { $pass = TRUE; }
if (preg_match("/\[\.\.\.\]/i", "$content")) { $pass = TRUE; }
if (preg_match('/[...]/i', "$content")) { $pass = TRUE; }
if (preg_match('/\[...\]/i', "$content")) { $pass = TRUE; }


More information about the thelist mailing list