pouncer Posted April 14, 2007 Share Posted April 14, 2007 $password = "274826"; how can i check if $password is a 6 digit number Quote Link to comment 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 } ?> Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 14, 2007 Author Share Posted April 14, 2007 perfect. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
maxic0 Posted April 14, 2007 Share Posted April 14, 2007 Thats great. Thank you! Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.