Regular expresions...
Moderator: General Moderators
Regular expresions...
Is it just me or are regexps one of, if not the hardest thing to learn about PHP?? Anyone know any decent tutorials?
Regexp's are not paticular to PHP, most parsing-type languange use them as do many shell programs. I haven't seen many good tutorials on the web, but I do like the O'Reilly Mastering Regular Expressions book. If you think of yourself as a computer scientist and not a programmer, you might want to check out a textbook that covers them. Typically speaking texts that worry about parsing, computability, or automata will cover regular expresssions.
Look at this article:
http://www.phpbuilder.com/columns/dario19990616.php3
It's kinda old, but it realy opens your mind.
http://www.phpbuilder.com/columns/dario19990616.php3
It's kinda old, but it realy opens your mind.
so with regular expressions could you check if the user entered ONLY a-z and 0-9 everything else like '"@{£$%$_£"%" spits back a message saying i duno.. "you have entered invalid characters". How would you do this?
I did this. It sort of worked but gave inconsistant results...
I did this. It sort of worked but gave inconsistant results...
Code: Select all
if(!ereg("[a-zA-Z]{,}", $_POST["username"])){
$errors[] = "You have entered <b>invalid characters</b>";
}Code: Select all
eregi("^[a-z0-9]$", $_POST["username"]) // this will check if in the whole string you have a-z A-Z or 0-9Use this:
Code: Select all
if(!eregi("^[a-z0-9]+$", $_POST["username"])){
$errors[] = "You have entered <b>invalid characters</b>";
}I have recommended the use of :
Ref http://www.weitz.de/regex-coach/
Both for learning and debugging regex's, only issue is that it only does Perl Compatible regex's, but great non-the-less.
Regards,
Ref http://www.weitz.de/regex-coach/
Both for learning and debugging regex's, only issue is that it only does Perl Compatible regex's, but great non-the-less.
Regards,
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
oops. i think that was started by something i mentioned. i'm sorry. i thought that my post would be relevant here and since it was in the same vein that it should need a new thread.twigletmac wrote:Sorry 'bout not noticing that before - I've trimmed out the other discussion, time for a little chastisement.m@ndio wrote:oy! you hijacked my post
Mac
i was just trying get help on something similar since it looked like it was along the same lines.
i actually found the write up in the orielly learning pperl really good for learning regular expressions.. so much so that i first learned something else, butt for somereason seem to have overwrote that in my mind and write regular experssions like i'm writing for perl then go find the rules for the language i'm in if it's not perl and make any necessary adjustments