Re: regex usage

From: Date: Mon, 18 Feb 2008 16:57:56 +0000
Subject: Re: regex usage
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-269493@lists.php.net to get a copy of this message
On Sun, February 17, 2008 9:34 am, Valedol wrote: > Is there a mothod to check string`s length with regex or the only way > is > using strlen? > > I want string consisting of 4 digits > and check string with this code: > > if (preg_match("/\d{4}/",$_POST[id])) > { echo $_POST[id]; } > > but preg_match returns true when string consists of 4 or more digits Actually, it will return true for anthing with 4 consecutive digits anywhere inside it, including: abc1234def You want: "/^\d{4}\$/" if you want EXACTLY 4 digits and nothing more. ^ anchors the string at the start $ anchors the string at the end The \ in front of $ is because $ is PHP for a variable, and while there are not characters after it, so it's NOT a variable today, you might add some on another day... -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/from/lynch Yeah, I get a buck. So?

« previous php.general (#269493) next »