web4 Posted June 19, 2010 Share Posted June 19, 2010 Hi i've been thinkign this in 1 day already and i think i need some help Here's a code that need to be revised <?php /*** a simple string ***/ $string = 'samples7'; /*** try to match letters with single number in it but limited to 8 characters and not less than 5 characters ***/ if(preg_match("/([a-zA-Z]+?=\d){5,8}$/", $string)) { /*** if we find the word white, not followed by house ***/ echo 'Found a match'; } else { /*** if no match is found ***/ echo 'No match found'; } ?> How could i revised that code to work correctly? Thanks Link to comment https://forums.phpfreaks.com/topic/205227-checking-string-with-only-one-digit/ Share on other sites More sharing options...
cags Posted June 19, 2010 Share Posted June 19, 2010 Something like this ought to work... $pattern = '#^(?=[a-z]*[0-9][a-z]*$).{5,8}$#i'; Link to comment https://forums.phpfreaks.com/topic/205227-checking-string-with-only-one-digit/#findComment-1074319 Share on other sites More sharing options...
ZachMEdwards Posted June 20, 2010 Share Posted June 20, 2010 Try this pattern: $pattern = '/(.[^\d]{4,7}\d)/'; Link to comment https://forums.phpfreaks.com/topic/205227-checking-string-with-only-one-digit/#findComment-1074713 Share on other sites More sharing options...
cags Posted June 20, 2010 Share Posted June 20, 2010 That will match a pattern that start with anything, followed by 4 - 7 non-numbers followed by a number, meaning it will allow it to start and end with a number but have no other numbers (i.e. have two in, which is not what was requested), or only one number which must be at the end. Link to comment https://forums.phpfreaks.com/topic/205227-checking-string-with-only-one-digit/#findComment-1074714 Share on other sites More sharing options...
salathe Posted June 21, 2010 Share Posted June 21, 2010 web4, did you get this sorted out? Of the two patterns offered, I'd go with cags' (since, it would work as you want). Link to comment https://forums.phpfreaks.com/topic/205227-checking-string-with-only-one-digit/#findComment-1074964 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.