Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/162281-solved-need-help-with-regex/
Share on other sites

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

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.

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

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

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.

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.  ;)

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

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.

Guest
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.