Regular expresions...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Regular expresions...

Post by m@ndio »

Is it just me or are regexps one of, if not the hardest thing to learn about PHP?? Anyone know any decent tutorials?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

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.
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

Look at this article:

http://www.phpbuilder.com/columns/dario19990616.php3

It's kinda old, but it realy opens your mind.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Yeah, that's a great intro. I had forgotten about it. It doesn't cover forward/back references and "capturing" matches, but its good to start with. If you get through it and still need more complexity then check out the O'Reilly book.
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

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

Code: Select all

if(!ereg("[a-zA-Z]{,}", $_POST["username"])){
		$errors[] = "You have entered <b>invalid characters</b>";		
	}
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

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-9
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

I forgot who on this board said this: "Regular expressions were created by evil programmers to give you a headache."

Truer words were never spoken, yet with great headache comes great power :P
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

Use this:

Code: Select all

if(!eregi("^[a-z0-9]+$", $_POST["username"])){
      $errors[] = "You have entered <b>invalid characters</b>";      
   }
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

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,
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Post by Gleeb »

Microsoft have a good reference in the MDSN library that I use. It makes references to Javascript and VBScript, but regex is pretty standardised between platforms.
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

oy! you hijacked my post :o
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

m@ndio wrote:oy! you hijacked my post :o
Sorry 'bout not noticing that before - I've trimmed out the other discussion, time for a little chastisement.

Mac
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

cheers twig
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

twigletmac wrote:
m@ndio wrote:oy! you hijacked my post :o
Sorry 'bout not noticing that before - I've trimmed out the other discussion, time for a little chastisement.

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

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
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

no worries, I just didn't get the answer I wanted in my post that's all.
Post Reply