What's this code line mean?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
camarosource
Forum Commoner
Posts: 77
Joined: Sat Aug 03, 2002 10:43 pm

What's this code line mean?

Post by camarosource »

if (preg_match ("/2\D\d\d\D[2-9|A]\w\d\d\d\d\d\d/", $vin))

I understand preg_match is used to match chars in the variable, in this case "$vin". But don't understand what exactly it's matching "/2\D\d\d\D[2-9|A]\w\d\d\d\d\d\d/"

Thanks
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

I would say that's some ID-like. Maybe some account number (but not bank account). Here's something which will clear your mind ->

http://php.net/manual/en/pcre.pattern.syntax.php

and I also think it will accept the following string 2-12CAPHP123456
camarosource
Forum Commoner
Posts: 77
Joined: Sat Aug 03, 2002 10:43 pm

Post by camarosource »

thanks..
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post by will »

delorian wrote:I would say that's some ID-like. Maybe some account number (but not bank account). Here's something which will clear your mind ->

http://php.net/manual/en/pcre.pattern.syntax.php

and I also think it will accept the following string 2-12CAPHP123456

well, close. \w matches a word character, not an actual word. from the link you provided..
A "word" character is any letter or digit or the underscore character, that is, any character which can be part of a Perl "word". The definition of letters and digits is controlled by PCRE's character tables, and may vary if locale-specific matching is taking place (see "Locale support" above). For example, in the "fr" (French) locale, some char- acter codes greater than 128 are used for accented letters, and these are matched by \w.
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

will wrote:well, close. \w matches a word character, not an actual word. from the link you provided..
Yea, you're right, my mistake.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Re: What's this code line mean?

Post by m3rajk »

i get the feeling i know the perl shorts better than the rest.
camarosource wrote:if (preg_match ("/2\D\d\d\D[2-9|A]\w\d\d\d\d\d\d/", $vin))
take $vin and checks that it has a section (not necessarily the whole string, not necessarily the begining OR ending) that starts with the number two, followed by character that's not a digit, followed by two digits, followed by any non-digit, followed by a digit that's in the range 2-9 inclusive OR the letter A (and it has to be capitalized)followed by any word character followed by 6 digits

it could be switched to if (preg_match ("/2\D\d{2}\D[2-9|A]\w\d{6}/", $vin)) without any change

the perl shorts are:
\w any character in the following range: A-Za-z-0-9_
\W anything not in \w
\d any character in the rage: 0-9
\D anything not in \d
\s any "white space" character
\S anything not in \s
delorian wrote:and I also think it will accept the following string 2-12CAPHP123456
it wont match this. "HP123456" is the part that's the dviation form the pattern. on the other hand 2-12CAP123456 will match
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Re: What's this code line mean?

Post by delorian »

m3rajk wrote:
delorian wrote:and I also think it will accept the following string 2-12CAPHP123456
it wont match this. "HP123456" is the part that's the dviation form the pattern. on the other hand 2-12CAP123456 will match
I already know that m3rajk, user will already told me that, and I said that was my mistake, right... :D
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Re: What's this code line mean?

Post by m3rajk »

delorian wrote:
m3rajk wrote:
delorian wrote:and I also think it will accept the following string 2-12CAPHP123456
it wont match this. "HP123456" is the part that's the dviation form the pattern. on the other hand 2-12CAP123456 will match
I already know that m3rajk, user will already told me that, and I said that was my mistake, right... :D
i wasn't sure if you meant caphp as the word or php so i wasn't sure which you took it as, thats why i decided to mention where you go off the actual patten looked for
Post Reply