jamesxg1 Posted December 17, 2010 Share Posted December 17, 2010 Hiya peeps! I have this code; if(preg_match('([0-9]{5})', $this->_searchString)) { echo '5 characters, numbers only.'; } elseif(preg_match('([0-9]{7})', $this->_searchString)) { echo '7 characters, numbers only.'; } The 7 characters are not working for some reason, I have used this preg_match regex all over my script and it's working everywhere else but with this? Can anyone see any reason why it is not working? Many thanks, James. Link to comment https://forums.phpfreaks.com/topic/221930-preg_match-working-in-one-place-but-not-another/ Share on other sites More sharing options...
requinix Posted December 17, 2010 Share Posted December 17, 2010 If there are 7 numbers then there are also 5 numbers. The first condition will match. Link to comment https://forums.phpfreaks.com/topic/221930-preg_match-working-in-one-place-but-not-another/#findComment-1148459 Share on other sites More sharing options...
johnny86 Posted December 17, 2010 Share Posted December 17, 2010 You should check for 7 digits first. Because this [0-9]{5} will match: 12345 AND 1234567 therefore your script never reaches the 1234567 ( [0-9]{7} ) because 5 digits is matched first. Link to comment https://forums.phpfreaks.com/topic/221930-preg_match-working-in-one-place-but-not-another/#findComment-1148460 Share on other sites More sharing options...
jamesxg1 Posted December 17, 2010 Author Share Posted December 17, 2010 You should check for 7 digits first. Because this [0-9]{5} will match: 12345 AND 1234567 therefore your script never reaches the 1234567 ( [0-9]{7} ) because 5 digits is matched first. Wow! I honestly didn't think of that! If I add $ at the end of each regex will this stop that? Many thanks, James. Link to comment https://forums.phpfreaks.com/topic/221930-preg_match-working-in-one-place-but-not-another/#findComment-1148463 Share on other sites More sharing options...
jamesxg1 Posted December 17, 2010 Author Share Posted December 17, 2010 Thanks for your help guys, pointing that out helped me fix it Many thanks, James. Link to comment https://forums.phpfreaks.com/topic/221930-preg_match-working-in-one-place-but-not-another/#findComment-1148465 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.