Re: regex usage

From: Date: Sun, 17 Feb 2008 17:45:05 +0000
Subject: Re: regex usage
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-269439@lists.php.net to get a copy of this message
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
Yes you can, but why do it this way? What advantage is it over the following? Given that this isn't a real intense regex, I don't know how much you will save, but you could also do this. if ( strlen(intval($_POST['id'])) == 4 ) { echo $_POST['id']; } Not sure about speed, but it might be a little faster if you are looking for performance. Jim Lucas

« previous php.general (#269439) next »