Andy-H Posted June 15, 2009 Share Posted June 15, 2009 Can anyone help me to make a pattern to check a variable with preg_match to ensure that a string starts with an alpha-numeric character, contains only [A-Za-z0-9.-_] and is no longer than 15 characters, i tried but failed with the following: if ( preg_match("/^[A-Za-z]{1,}[A-Za-z0-9._-]?$/ i", $user) ) { ///etc Thanks. Quote Link to comment Share on other sites More sharing options...
Maq Posted June 15, 2009 Share Posted June 15, 2009 This makes the minimal 2, did you have a specific setting? Try this: if(preg_match("/^[a-z\d][\w-.]{1,14}/ i", $user) ) Quote Link to comment Share on other sites More sharing options...
.josh Posted June 15, 2009 Share Posted June 15, 2009 Actually Maq, you'd want to throw a $ onto the end of the pattern, and you'd want to move the hyphen to either the beginning or the end of the class. Also, to avoid potential issues from local settings, it would be better to not use the shorthand \d and \w Quote Link to comment Share on other sites More sharing options...
Maq Posted June 15, 2009 Share Posted June 15, 2009 Actually Maq, you'd want to throw a $ onto the end of the pattern, and you'd want to move the hyphen to either the beginning or the end of the class. I understand the concern about the hyphen, but why would it be necessary to add a bang to the end?. Gotcha now, I was being a little dense earlier. Thx CV. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 15, 2009 Author Share Posted June 15, 2009 Sorry, I meant so that it has to start with an alpha character, [A-Za-z] and can be followed by up to 14 alpha-numric chars and tbh it would probably be best if it can only contain either 1 dot, dash or underscore. if (preg_match("/^[a-z][a-z\d[.-_]{0,1}]{1,14}/ i", $user) ) Sorry about that, just had a little re-think and if users used say, 4 dots in their nae it would be a bit annoying lol. Anyhow, is that correct? Thanks Quote Link to comment Share on other sites More sharing options...
salathe Posted June 15, 2009 Share Posted June 15, 2009 First, I know this thread is marked as SOLVED already. I'm new to the forum and if it is considered bad etiquette to reply to solved threads then please do let me know! On to the reply. It would be possible to construct a regular expression to check the conditions (as you requested): Starts with alphabetical character Contains only alphanumeric, ".", "-" or "_" characters Has a maximum length of 15 characters Contains only one ".", "-" or "_" character However that pattern might not be particularly understandable at a glance (or even after careful consideration!) when looking through the code. My suggestion would be to break things down into at least two sections. [*]Have a nice, simple regex to check that the string starts with [a-z], only contains [a-zA-Z0-9.-_] and has a max. length of 15 characters. [*]Check for only one ".", "-" or "_" character. That could be achieved by: if (preg_match('/^[a-z][a-z0-9._-]{0,14}$/Di', $user) && preg_match_all('/[._-]/', $user, $matches) <= 1) { // ... } If you must stick to only one regular expression (perhaps just for educational purposes) then you might use the following if you want to have a stab at deciphering what it's doing! /^(?=[a-z0-9]*+[._-]?[a-z0-9]*+$)[a-z][a-z0-9._-]{0,14}$/Di Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted June 15, 2009 Share Posted June 15, 2009 In addition to CV's comments about locales and the problems poised by character class short hands such as d and w for example, you could do what CV suggested (that is, explicitly set your own character classes), or add setlocale(LC_CTYPE, 'C'); prior to any regex (this will handle stuff like d or w 'as advertised' so to speak). While I'm not saying 'My way is right and everyone else's is wrong', this is the approach I personally prefer, as I still like working with those short hand character classes, as I find them to be less cluttering in the regex patterns. Besides, functions like ctype_alnum, ctype_digit and the like also fall prey to locale issues just as d and w do. So by setting your locale LC_CTYPE to 'C' first, you should get the expected results be it via regex or ctype functions. But either way, indeed be mindful about the pitfalls of locales as CV mentioned. Quote Link to comment Share on other sites More sharing options...
Maq Posted June 15, 2009 Share Posted June 15, 2009 First' date=' I know this thread is marked as SOLVED already. I'm new to the forum and if it is considered bad etiquette to reply to solved threads then please do let me know! On to the reply.[/quote'] No please, 'post-solved' ideas, comments, and questions are very much welcome, that is, if they aren't repetitive and/or useless. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 16, 2009 Author Share Posted June 16, 2009 Thanks everyone, I had initially gone with if ( !ctype_alpha( substr($user, 0, 1) ) || !ctype_alnum($user) || strLen($user) < 3 || strLen($user) > 15 ) { As I find regular expressions complicated and hard to grasp. Although I think I'll just copy and paste the code this time, due to reasons previously stated lol Thanks again eveyone. =D Quote Link to comment 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.