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

Dan Parry dan at shaded.plus.com
Tue May 15 05:50:55 CDT 2012


Hi Bob

Just tested this and using the following code

$content = "apple pie [...] strudel cookies"; if (preg_match('/\[\.+\]/i', $content)) { $pass = true; } else { $pass = false; }

I get true for that string, but if I alter the [...] then I get false. Actually, that regex allows for as many or few (minimum 1) full-stops between the square brackets

If you want to ONLY allow 3 full-stops then change the + to {3}

HTH

Dan

-----Original Message-----
From: thelist-bounces at lists.evolt.org [mailto:thelist-bounces at lists.evolt.org] On Behalf Of Bob Meetin
Sent: 15 May 2012 07:49
To: thelist at lists.evolt.org
Subject: [thelist] PHP - preg_match - brackets [...]

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.

i.e.

<?php
$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; }

-Bob
-- 

* * Please support the community that supports you.  * * http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester and archives of thelist go to: http://lists.evolt.org Workers of the Web, evolt ! 


More information about the thelist mailing list