Jump to content


Photo

preg_match


  • Please log in to reply
9 replies to this topic

#1 wolves

wolves
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 21 March 2006 - 01:00 PM

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

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 21 March 2006 - 01:07 PM

[!--quoteo(post=356939:date=Mar 21 2006, 01:00 PM:name=wolves)--][div class=\'quotetop\']QUOTE(wolves @ Mar 21 2006, 01:00 PM) View Post[/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
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 wolves

wolves
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 21 March 2006 - 01:10 PM

Sorry....

I'm using preg_match('/[0-9]{2}\/[0-9]{2}\/[0-9]{2}/', '01/01/2005');

and it's return true


#4 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 21 March 2006 - 01:22 PM

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:
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);

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
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#5 wolves

wolves
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 21 March 2006 - 01:27 PM

humn, I think I understood;

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

#6 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 21 March 2006 - 01:32 PM

[!--quoteo(post=356952:date=Mar 21 2006, 01:27 PM:name=wolves)--][div class=\'quotetop\']QUOTE(wolves @ Mar 21 2006, 01:27 PM) View Post[/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:

$match = preg_match('/[0-9]{2}\/[0-9]{2}\/[0-9]{2}[\ \n\r]/', '01/01/2005', $matches);

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.

EDIT: the bit i added is [\ \n\r] - the 'space' does not appear clearly in my code snippet.
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#7 wolves

wolves
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 21 March 2006 - 01:44 PM

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?

#8 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 21 March 2006 - 01:58 PM

[!--quoteo(post=356960:date=Mar 21 2006, 01:44 PM:name=wolves)--][div class=\'quotetop\']QUOTE(wolves @ Mar 21 2006, 01:44 PM) View Post[/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.

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#9 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 21 March 2006 - 08:07 PM

preg_match('/^[0-9]{2}\/[0-9]{2}\/[0-9]{2}$/', '01/01/2005' );

^ means "beginning of string"
$ means "end of string"

So if you want to match the whole string, use them both.

#10 wolves

wolves
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 28 March 2006 - 06:57 PM

tks, It's solved my problem! :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users