Re: preg_match

From: Date: Sat, 01 Apr 2006 05:07:01 +0000
Subject: Re: preg_match
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-233039@lists.php.net to get a copy of this message
Benjamin D Adams wrote:
I'm trying to check a string for ../ <?php if(preg_match("/..//i", $string)){ echo "string has ../"; } ?> Can't get it to work can anyone help?
That's terrible overkill. Regex is not designed for simple substring matching. You want: if( strpos( $string, '../' ) !== false ) echo 'string has ../'; By the way, your problem is that . is a special character in regular expressions, so needs escaping with a backslash, and you have used / as your delimiter but also use it inside the pattern. You should use a different delimiter (also, there's no point in using the 'i' case-insensitive flag, since there's no characters in your pattern). The strpos() solution above is much better and faster in this case, though. -- Jasper Bryant-Greene General Manager Album Limited http://www.album.co.nz/ 0800 4 ALBUM jasper@album.co.nz 021 708 334

« previous php.general (#233039) next »