PHP & RegEx help :(

Need ti make a safe regex, dont have a clue how!

Page 1 of 1

6 Replies - 1501 Views - Last Post: 16 April 2009 - 07:23 PM Rate Topic: -----

#1 huzi8t9  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 367
  • Joined: 11-July 07

PHP & RegEx help :(

Post icon  Posted 19 September 2008 - 12:44 PM

Hello, I'm currently making a website where Users can register. I'm trying to cover my own backside by maing it so users are limited to what they may and may no have in their usernames.

Here's my code:

<?
$pattern = '/^[^0-9][A-z0-9_]+([A-z0-9_]+)$/';
$User = $_POST['User'];
$Err = new Array();
if(!preg_match($pattern,$User))
{
	$Err[] = 'The <b>username</b> you\'ve selected has invalid characters.';
}
?>
...
...



What does that allow or disallow. I figured that out reading through a tutorial just putting bit and bobs in random places, then moving it about because it didn't work. I would dearly appreciate it if someone could write a tutorial on DreamInCode if no one has already.

Any help greatly appreciated, Mark.

This post has been edited by huzi8t9: 19 September 2008 - 12:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: PHP & RegEx help :(

#2 mocker  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: PHP & RegEx help :(

Posted 19 September 2008 - 01:39 PM

What do you want your regex to do? It looks like you want a regex that begins with anything not a digit (\D does the same thing), then with 1 to any amount of alphanumeric characters or underscores, and then more alphanumeric characters till the end of the string .

/^\D[\w\d\_]+$/ should be the same as you've written

if you want only alphanumeric or underscores in the entire thing, check for:
/^[\w\d\_]+$/

if you want to see if its alphanumeric/underscore with a single digit at the beginning, check for
/^\d[\w\d\_]+$/

This is one of my favorite regex tutorials:
http://www.anaesthet...x.htm#regex.htm

preg_match stands for perl-style regular expressions, so searching for "perl regular expressions" will give you a ton of results
Was This Post Helpful? 0
  • +
  • -

#3 huzi8t9  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 367
  • Joined: 11-July 07

Re: PHP & RegEx help :(

Posted 08 October 2008 - 11:18 PM

View Postmocker, on 19 Sep, 2008 - 01:39 PM, said:

What do you want your regex to do? It looks like you want a regex that begins with anything not a digit (\D does the same thing), then with 1 to any amount of alphanumeric characters or underscores, and then more alphanumeric characters till the end of the string .

/^\D[\w\d\_]+$/ should be the same as you've written

if you want only alphanumeric or underscores in the entire thing, check for:
/^[\w\d\_]+$/

if you want to see if its alphanumeric/underscore with a single digit at the beginning, check for
/^\d[\w\d\_]+$/

This is one of my favorite regex tutorials:
http://www.anaesthet...x.htm#regex.htm

preg_match stands for perl-style regular expressions, so searching for "perl regular expressions" will give you a ton of results


Thankyou for your help, I'm trying to look at your code and see what at where the magic happens. Hopfully, I'll understand it.. Sooner or later!!

Thankyou!
Was This Post Helpful? 0
  • +
  • -

#4 DilutedImage  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 646
  • Joined: 20-November 06

Re: PHP & RegEx help :(

Posted 09 October 2008 - 12:30 AM

\w is equivalent to [a-zA-Z0-9_] . So, those patterns could be rewritten as:

/^\D\w+$/
/^\w+$/
/^\d\w+$/

It should be mentioned though that \D checks for a non-digit characters. Special characters (!@#$%^&*+-_etc...) would all match \D. To enforce letters-only, use [a-zA-Z].

2

p.s. That's a great tutorial.

This post has been edited by DilutedImage: 09 October 2008 - 12:31 AM

Was This Post Helpful? 0
  • +
  • -

#5 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 798
  • Joined: 25-February 08

Re: PHP & RegEx help :(

Posted 09 October 2008 - 07:16 AM

Just to let you know the regex you had

$pattern = '/^[^0-9][A-z0-9_]+([A-z0-9_]+)$/';



means that a username cannot start with a number, must have at least 1 alphanumeric or underscore at the beginning, and then at least 1 alphanumeric or underscore at the end, with no limit to the max username length.

Disallowed:
0akozlik
0
k

Allowed:
_akozlik
_0akozlik
ak0zlik
ak

Hope that helps.

Also, I wrote a multi part regex tutorial that you might want to check out. It's not complete yet, but the first few parts are up.

Regex Part 1
Regex Part 2

Good luck!

*EDIT*
It's nice to see someone using regexes for their intended purpose; detecting patterns. A lot of people make the mistake of using regex as a substring function. Props for knowing what you're doing.

This post has been edited by akozlik: 09 October 2008 - 07:17 AM

Was This Post Helpful? 0
  • +
  • -

#6 huzi8t9  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 367
  • Joined: 11-July 07

Re: PHP & RegEx help :(

Posted 16 April 2009 - 05:19 PM

View Postakozlik, on 9 Oct, 2008 - 06:16 AM, said:

Just to let you know the regex you had

$pattern = '/^[^0-9][A-z0-9_]+([A-z0-9_]+)$/';



means that a username cannot start with a number, must have at least 1 alphanumeric or underscore at the beginning, and then at least 1 alphanumeric or underscore at the end, with no limit to the max username length.

Disallowed:
0akozlik
0
k

Allowed:
_akozlik
_0akozlik
ak0zlik
ak

Hope that helps.

Also, I wrote a multi part regex tutorial that you might want to check out. It's not complete yet, but the first few parts are up.

Regex Part 1
Regex Part 2

Good luck!

*EDIT*
It's nice to see someone using regexes for their intended purpose; detecting patterns. A lot of people make the mistake of using regex as a substring function. Props for knowing what you're doing.


Thankyou so much. Your tutorials are very very interesting, I still can't grasp the idea of that whole concept, I'm sure I'll get there in the end.

And again, thankyou VERY much!
Was This Post Helpful? 0
  • +
  • -

#7 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: PHP & RegEx help :(

Posted 16 April 2009 - 07:23 PM

Here is a great place to test regexs
http://regexpal.com/
so you can tell if something matches or not.
Regexs are an alphabet, they describe the what you are allowed to have in a string. What are you having difficulty understand? we can probably help explain it in another way if the tutorial doesn't make sense to you :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1