have got a clue about regex so need a hand,
what would the regex string look like if i wanted to match this:
a-z, A-Z, 0-9, - and _
thanks
pretty standard regex required please
Moderator: General Moderators
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
[a-zA-Z0-9_-]-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
this code..
returns
obviously, malcolm!!! should fail
Code: Select all
function checkUsernameAllowed ($username) {
$start = getMicrotime();
// firstly check its in the correct format
$regex = "[a-zA-Z0-9_-]";
if (ereg ($regex, $username)) {
$end = getMicrotime();
print "Username: {$username}\nvalid";
} else {
$end = getMicrotime();
print "Username: {$username}\ninvalid";
}
$total = number_format(($end - $start), 4);
print "\n{$total} to analyze for validity\n\n";
}
checkUsernameAllowed ("malcolmboston");
checkUsernameAllowed ("malcolm!!!");
?>Code: Select all
Username: malcolmboston
valid
0.0003 to analyze for validity
Username: malcolm!!!
valid
0.0001 to analyze for validity- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
No, it shouldn't. Why? The regex only looks for a single character match. If any character in the tested string match, the regex will say it found it.
will perform it over the entire string.
Code: Select all
if (preg_match('#^[a-zA-Z0-9_-]+$#', $username))-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK