Jump to content

Archived

This topic is now archived and is closed to further replies.

wolves

preg_match

Recommended Posts

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 :(

Share this post


Link to post
Share on other sites
[!--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.

Cheers
Mark

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
humn, I think I understood;

but how to match only if the year have to digits only?

Share this post


Link to post
Share on other sites
[!--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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[!--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.

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.