Jump to content

Using preg_match


Recommended Posts

I am trying to use preg_match to check whether a membership number has been entered in the form 'A123456'.

I have found a site on the web that shows the types of string syntax that can be used to check a string, so I have come up with the following:

preg_match ('/P.P/ \d{6}', $trimmed['BMFA_No'])

However when run I get the following error:

An error occurred in script D:\wamp\www\MFC1066\Reg1.php on line 19: preg_match(): Unknown modifier '\'

Obviously I have the syntax wrong, but as I have never used this command before I am unsure where this is wrong.

I am sure that someone out there is much more experienced that me and can solve this easily.

Link to comment
Share on other sites

I don't know what that /P.P/ thing is supposed to be because it doesn't look like anything close to what you need to use.


Be more specific about the membership number. What is the exact format? What are valid examples and why are they valid, then what are some invalid examples and why are they invalid?

Link to comment
Share on other sites

Membership number is as shown in the example. I.e. A123456

One letter (A to Z) and a 6 digit number.

However, could it be set to accept either upper-case letter and if the user left a space between the letter and the number.

By the way the /P.P/ thing according to the information on the web it matches a single character.

It's a pity that the PHP reference does not show the valid syntax for the preg_match command.

Link to comment
Share on other sites

RegEx is complicated. There is no way to boil down to a single page in the manual. You will need to learn it if you want to understand it. The \p is a delimiter for certain programming languages. It is not used in PHP.

$values = array(
    'a 123456',
    'A 123456',
    '1234567', //no letter
    'a12345', //less than 6 digits
    'a1234567' //greater than 6 digits
foreach($values as $value)
    $match = preg_match("#^[a-z] ?\d{6}$#i", $value) ? "Valid" : "Invalid";
    echo "{$value}: {$match}<br>\n";


a123456: Valid
A123456: Valid
a 123456: Valid
A 123456: Valid
1234567: Invalid
a12345: Invalid

a1234567: Invalid
Edited by Psycho
Link to comment
Share on other sites

Here is a break down of the expression.


#^[a-z] ?\d{6}$#i

The hashmarks are used to delineate the beginning and ending of the expression. The 'i' after the ending of the expression definition indicates that the expression should be case-insensitive.


#^[a-z] ?\d{6}$#i

The "^" and "$" indicate that the match must start from the beginning and end of the string (i.e. the match can't be inside the string)


#^[a-z] ?\d{6}$#i

The first character must match a character within the character class of "a-z"


#^[a-z] ?\d{6}$#i

The next character to match is a space, but the "?" indicates that it is optional


#^[a-z] ?\d{6}$#i

The "\d" is shorthand for any character that is a digit. The "(6)" following it means that there must be 6 instances of the preceding match.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

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.