Page 1 of 1

preg replace? str_replace ? function?

Posted: Tue Mar 08, 2005 4:33 am
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"

Posted: Tue Mar 08, 2005 4:49 am
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

Posted: Fri Mar 11, 2005 4:46 am
by scudz
thanks, worked a treat!

Posted: Fri Mar 11, 2005 6:17 am
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