preg replace? str_replace ? function?

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
scudz
Forum Newbie
Posts: 8
Joined: Tue Mar 08, 2005 4:27 am

preg replace? str_replace ? function?

Post by scudz »

I am currently developing a site and am pretty new to php. I have a form where a user can add a keyword (unique) to be used in the menu system

However, i would like the user to only be able to enter in this field, the letters A-Z, a-z and 0-9, with no other chars allowed.Any other chars should be just stripped out.

I currently do this... but its a poor way to do it...

Code: Select all

$keyword = str_replace ( '?', '', $keyword );
$keyword = str_replace ( '&', '', $keyword );
$keyword = str_replace ( '!', '', $keyword );
this list can get really long... any idea on a better way would be grateful.. thanks :>

an example string "hello!"$%%^^*" would just be "hello"
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yeah you need to learn regular expressions and do a preg_replace()

Code: Select all

$new_string = preg_replace('/ї^a-zA-Z ]/', '', $string);  //Add 0-9 to the brackets to allow numbers
scudz
Forum Newbie
Posts: 8
Joined: Tue Mar 08, 2005 4:27 am

Post by scudz »

thanks, worked a treat!
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

d11 already suggested a good reference in viewtopic.php?t=31465
Heres a nice understanding tutorial for newbies : http://www.phpbuilder.com/columns/dario ... hp3?aid=46
Post Reply