Re: [PHP] regex usage
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? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regex usage
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 preg_match(/^\d{4}$/,$_POST['id']) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regex usage
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 You could change the regex to use start/end anchors. Also, the trailing comma in the length specifier bit means 4 or more Eg: if (preg_match(/^\d{4,}$/, $_POST['id'])){ echo $_POST['id']; } -- Richard Heyes http://www.websupportsolutions.co.uk Knowledge Base and Helpdesk software hosted for you - no installation, no maintenance, new features automatic and free ** New Helpdesk demo now available ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regex usage
--- Valedol [EMAIL PROTECTED] 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 -- To check only 4 digit, you have to specify max lengh of the string in length specifier.. if (preg_match(/\d{4,4}/,$_POST[id])) { echo $_POST[id]; } --- Nirmalya Lahiri [+91-9433113536] Looking for last minute shopping deals? Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regex usage
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php