pouncer Posted April 14, 2007 Share Posted April 14, 2007 $password = "274826"; how can i check if $password is a 6 digit number Link to comment https://forums.phpfreaks.com/topic/46995-solved-checking-if-its-a-6-digit-random-number/ Share on other sites More sharing options...
obsidian Posted April 14, 2007 Share Posted April 14, 2007 Try this: <?php $password = '274826'; if (preg_match('|^[\d]{6}\z|', $password)) { // valid 6 digit number } else { // not a 6 digit number } ?> Link to comment https://forums.phpfreaks.com/topic/46995-solved-checking-if-its-a-6-digit-random-number/#findComment-229166 Share on other sites More sharing options...
pouncer Posted April 14, 2007 Author Share Posted April 14, 2007 perfect. Link to comment https://forums.phpfreaks.com/topic/46995-solved-checking-if-its-a-6-digit-random-number/#findComment-229193 Share on other sites More sharing options...
maxic0 Posted April 14, 2007 Share Posted April 14, 2007 Can someone explain to me what this means please: |^[\d]{6}\z| thanks Link to comment https://forums.phpfreaks.com/topic/46995-solved-checking-if-its-a-6-digit-random-number/#findComment-229196 Share on other sites More sharing options...
Glyde Posted April 14, 2007 Share Posted April 14, 2007 Explanation by parts: ^ - Matches the beginning of the string, this means that NO CHARACTERS will come before thepattern specified [\d] - A character set to match any integer {6} - Means to match 6 characters from the character set specified above \z - Matches the end of the string. This in conjunction with ^ means that only that specific pattern can be matched. If there are any characters before or after the match, it will count as not matching. This is really hard to explain, so I'll give some examples. 435345 - Pass - All integers 42453 - Fail - All integers, but it doesn't meet the "6" length requirement f45235 - Fail - There are 6 characters, but they are not all integers f435134 - Fail - There are 6 integers, but there are characters before the integers (the "f"). This is unacceptable with the use of ^ f874938u - Fail - See above. Also, there is a character at the end, which is unacceptable with \z That's the best explanation I can give you. Link to comment https://forums.phpfreaks.com/topic/46995-solved-checking-if-its-a-6-digit-random-number/#findComment-229198 Share on other sites More sharing options...
maxic0 Posted April 14, 2007 Share Posted April 14, 2007 Thats great. Thank you! Link to comment https://forums.phpfreaks.com/topic/46995-solved-checking-if-its-a-6-digit-random-number/#findComment-229202 Share on other sites More sharing options...
obsidian Posted April 14, 2007 Share Posted April 14, 2007 Explanation by parts: ^ - Matches the beginning of the string, this means that NO CHARACTERS will come before thepattern specified [\d] - A character set to match any integer {6} - Means to match 6 characters from the character set specified above \z - Matches the end of the string. This in conjunction with ^ means that only that specific pattern can be matched. If there are any characters before or after the match, it will count as not matching. This is really hard to explain, so I'll give some examples. 435345 - Pass - All integers 42453 - Fail - All integers, but it doesn't meet the "6" length requirement f45235 - Fail - There are 6 characters, but they are not all integers f435134 - Fail - There are 6 integers, but there are characters before the integers (the "f"). This is unacceptable with the use of ^ f874938u - Fail - See above. Also, there is a character at the end, which is unacceptable with \z That's the best explanation I can give you. Very well said. Topic marked solved. Link to comment https://forums.phpfreaks.com/topic/46995-solved-checking-if-its-a-6-digit-random-number/#findComment-229206 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.