Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,640 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,091 people online right now. Registration is fast and FREE... Join Now!




PHP & RegEx help :(

 
Reply to this topicStart new topic

PHP & RegEx help :(, Need ti make a safe regex, dont have a clue how!

huzi8t9
post 19 Sep, 2008 - 11:44 AM
Post #1


New D.I.C Head

*
Joined: 11 Jul, 2007
Posts: 23


My Contributions


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:

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 Sep, 2008 - 11:45 AM
User is offlineProfile CardPM

Go to the top of the page

mocker
post 19 Sep, 2008 - 12:39 PM
Post #2


D.I.C Regular

Group Icon
Joined: 14 Oct, 2007
Posts: 256



Thanked 15 times
My Contributions


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.anaesthetist.com/mnm/perl/Findex.htm#regex.htm

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

Go to the top of the page

huzi8t9
post 8 Oct, 2008 - 10:18 PM
Post #3


New D.I.C Head

*
Joined: 11 Jul, 2007
Posts: 23


My Contributions


QUOTE(mocker @ 19 Sep, 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.anaesthetist.com/mnm/perl/Findex.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!
User is offlineProfile CardPM

Go to the top of the page

DilutedImage
post 8 Oct, 2008 - 11:30 PM
Post #4


D.I.C Addict

Group Icon
Joined: 20 Nov, 2006
Posts: 642



Thanked 6 times

Dream Kudos: 25
My Contributions


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

CODE
/^\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].



p.s. That's a great tutorial.

This post has been edited by DilutedImage: 8 Oct, 2008 - 11:31 PM
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 9 Oct, 2008 - 06:16 AM
Post #5


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


Just to let you know the regex you had

php

$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: 9 Oct, 2008 - 06:17 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 04:34AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month