rocky48 Posted November 12, 2016 Share Posted November 12, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302520-using-preg_match/ Share on other sites More sharing options...
requinix Posted November 12, 2016 Share Posted November 12, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/302520-using-preg_match/#findComment-1539247 Share on other sites More sharing options...
rocky48 Posted November 12, 2016 Author Share Posted November 12, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302520-using-preg_match/#findComment-1539249 Share on other sites More sharing options...
Psycho Posted November 12, 2016 Share Posted November 12, 2016 (edited) 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( 'a123456', 'A123456', '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"; } Output a123456: Valid A123456: Valid a 123456: Valid A 123456: Valid 1234567: Invalid a12345: Invalid a1234567: Invalid Edited November 12, 2016 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/302520-using-preg_match/#findComment-1539250 Share on other sites More sharing options...
Psycho Posted November 12, 2016 Share Posted November 12, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302520-using-preg_match/#findComment-1539252 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.