code for doing a search from a database

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
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You are correct. It should be:

'/[^a-zA-Z]/' is all characters that are not a-z or A-Z
'/[^0-9\-]/' is all characters that are not 0-9 or dash
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

arborint wrote:You are correct. It should be:

'/[^a-zA-Z]/' is all characters that are not a-z or A-Z
'/[^0-9\-]/' is all characters that are not 0-9 or dash
only matches 1 character.. any character in the string.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

No, it will remove all occurances that are not in the set given. For example:

$str = preg_replace('/[^a-zA-Z]/', '', $str);

will remove all characters except letters. It's probably the most generic way to filter strings.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

with replace yes, I was talking about matching.
Post Reply