how can i verify the validity with the php pregmatch function. The string starts with a year, then a dash, followed by between 1-3 numbers, another dash,then betweek 1-5 letters, another dash, thenmore letters between 1-10, another dash and finally between 1-3 letters for example:

2007-03-DPE-MSK-001

thanks

how can i verify the validity with the php pregmatch function. The string starts with a year, then a dash, followed by between 1-3 numbers, another dash,then betweek 1-5 letters, another dash, thenmore letters between 1-10, another dash and finally between 1-3 letters for example:

2007-03-DPE-MSK-001

thanks

preg_match() uses a regular expression (pattern) to find matches within a string. It uses the Perl compatible Regular Expressions (PCRE).

http://www.php.net/preg_match

You can find good information on Regular Expressions (Regex) here:
http://www.regular-expressions.info/

It's quite simple to learn once you get the basic idea behind it, and is a huge part of programming - a very powerful tool.

A regular expression that would match 4 digits is:

/^[0-9]{4}$/

Matching 3 uppercase alpha-characters only is:

/^[A-Z]{3}/

The '-' needs to be escaped, as it has special meaning. The escape character is '\'. It makes the next character meaningless.. (a literal).


So if you wanted to match "MSK-001" you'd use the regex expression:

"/^[A-Z]{3}\-[0-9]{3}$/"

Hope that helps you solve the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.