wolves Posted March 21, 2006 Share Posted March 21, 2006 I'm trying to use, preg_match to find a string like this '01/01/05'so I'm using preg_match('/[0-9]{2}\/[0-9]{2}\/[0-9]{2}/', '01/01/05' );but If I pass '01/01/2005' preg math returns true :( Quote Link to comment https://forums.phpfreaks.com/topic/5417-preg_match/ Share on other sites More sharing options...
redbullmarky Posted March 21, 2006 Share Posted March 21, 2006 [!--quoteo(post=356939:date=Mar 21 2006, 01:00 PM:name=wolves)--][div class=\'quotetop\']QUOTE(wolves @ Mar 21 2006, 01:00 PM) [snapback]356939[/snapback][/div][div class=\'quotemain\'][!--quotec--]I'm trying to use, preg_match to find a string like this '01/01/05'so I'm using preg_match('/[0-9]{2}\/[0-9]{2}\/[0-9]{2}/', '01/01/05' );but If I pass '01/01/2005' preg math returns true :([/quote]that's because it's actually finding exactly what youre after, regardless of what's contained each side of it.when you run the above preg_match, what it sees/finds is: 01/01/20 , which actually meets the criteria you've set it.all you can really do is test for whitespace after the date (or an end of line) or just validate the results when you have them to make sure that the date was entered in 2digit year format.CheersMark Quote Link to comment https://forums.phpfreaks.com/topic/5417-preg_match/#findComment-19317 Share on other sites More sharing options...
wolves Posted March 21, 2006 Author Share Posted March 21, 2006 Sorry....I'm using preg_match('/[0-9]{2}\/[0-9]{2}\/[0-9]{2}/', '01/01/[b]2005[/b]');and it's return true Quote Link to comment https://forums.phpfreaks.com/topic/5417-preg_match/#findComment-19319 Share on other sites More sharing options...
redbullmarky Posted March 21, 2006 Share Posted March 21, 2006 yes but it IS true. preg_match will use your pattern to find something ANYWHERE within a give string. so your match is actually matching correctly, and ignoring the 05 bit of your '2005' and matching with '01/01/20' which fits your criteria. you could put 2000000532 as the year, and it'd still come back true.try it:[code]echo 'matching 01/01/2005:<br>';$match = preg_match('/[0-9]{2}\/[0-9]{2}\/[0-9]{2}/', '01/01/2005', $matches);print_r($matches);echo '<br><br>Matching 01/01/12547612345:<br>';$match = preg_match('/[0-9]{2}\/[0-9]{2}\/[0-9]{2}/', '01/01/12547612345', $matches);print_r($matches);[/code]both of which will come back true. the first will show '01/01/20' as a match, the second will show '01/01/12' as a match.Cheers Quote Link to comment https://forums.phpfreaks.com/topic/5417-preg_match/#findComment-19326 Share on other sites More sharing options...
wolves Posted March 21, 2006 Author Share Posted March 21, 2006 humn, I think I understood;but how to match only if the year have to digits only? Quote Link to comment https://forums.phpfreaks.com/topic/5417-preg_match/#findComment-19327 Share on other sites More sharing options...
redbullmarky Posted March 21, 2006 Share Posted March 21, 2006 [!--quoteo(post=356952:date=Mar 21 2006, 01:27 PM:name=wolves)--][div class=\'quotetop\']QUOTE(wolves @ Mar 21 2006, 01:27 PM) [snapback]356952[/snapback][/div][div class=\'quotemain\'][!--quotec--]humn, I think I understood;but how to match only if the year have to digits only?[/quote]hmmm someone else will need to help on this one, but you basically need to test for an end character (such as a space, new line, etc. can't quite remember what the character is. something like:[code]$match = preg_match('/[0-9]{2}\/[0-9]{2}\/[0-9]{2}[\ \n\r]/', '01/01/2005', $matches);[/code]may work but i'm sure there's a better way to check for the end of line. either way, you just have to check for a non-numerical character DIRECTLY after your year.[b]EDIT:[/b] the bit i added is [\ \n\r] - the 'space' does not appear clearly in my code snippet. Quote Link to comment https://forums.phpfreaks.com/topic/5417-preg_match/#findComment-19330 Share on other sites More sharing options...
wolves Posted March 21, 2006 Author Share Posted March 21, 2006 I can be the last char of the memory? I don't know if preg_math serachs at the memory...what char is the last of a string in the memory? Quote Link to comment https://forums.phpfreaks.com/topic/5417-preg_match/#findComment-19334 Share on other sites More sharing options...
redbullmarky Posted March 21, 2006 Share Posted March 21, 2006 [!--quoteo(post=356960:date=Mar 21 2006, 01:44 PM:name=wolves)--][div class=\'quotetop\']QUOTE(wolves @ Mar 21 2006, 01:44 PM) [snapback]356960[/snapback][/div][div class=\'quotemain\'][!--quotec--]I can be the last char of the memory? I don't know if preg_math serachs at the memory...what char is the last of a string in the memory?[/quote]i'm honestly not sure. there's a special character used to match any whitespace character, regardless. if someone else can tell you what it is, that'll be what you need. Quote Link to comment https://forums.phpfreaks.com/topic/5417-preg_match/#findComment-19338 Share on other sites More sharing options...
wickning1 Posted March 21, 2006 Share Posted March 21, 2006 [code]preg_match('/^[0-9]{2}\/[0-9]{2}\/[0-9]{2}$/', '01/01/2005' );[/code]^ means "beginning of string"$ means "end of string"So if you want to match the whole string, use them both. Quote Link to comment https://forums.phpfreaks.com/topic/5417-preg_match/#findComment-19437 Share on other sites More sharing options...
wolves Posted March 28, 2006 Author Share Posted March 28, 2006 tks, It's solved my problem! :) Quote Link to comment https://forums.phpfreaks.com/topic/5417-preg_match/#findComment-21693 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.