Regex for controlling non-letter/number charcters

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

Post Reply
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Regex for controlling non-letter/number charcters

Post by neophyte »

I can't seem to find directions for controlling non-letter/number characters($#&@^!*()><?":{}) in regular expressions. Can someone enlighten me.

Example

A preg match expression that tests true for numbers only no special charcters no letters

/[^a-zA-Z]/

That locks out letters, how do you exclude the other characters?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$only_numbers = (bool)preg_match( '#^&#1111;0-9]+$#', $string );
is_numeric() can be used as well..
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Okay

Post by neophyte »

I get it. Once a noob always a noob. :oops:

Okay so the difference is you added the marks for begin of string "^" followed by the character class definition and end of string "$". With out the beginning and end the string will only test if the string has numbers.

Alright I think I'm getting the hang of this... :roll:

Thanks Feyd! :wink:
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

a book i can strongly recommend you:

Mastering Regular Expressions
Powerful Techniques for Perl and Other Tools
Jeffrey E.F. Friedl
O'REILLY
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yes.. a wonderful book I have recommended many times. Anything O'Reilly, I usually say. :)
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Thanks for the tip guys... I'll have to check out that book. Thanks again
Post Reply