Forum Moderators: coopster

Message Too Old, No Replies

quicker way of doing this with array

         

jackvull

12:03 pm on Nov 17, 2005 (gmt 0)

10+ Year Member



I have a function on my site to remove any of the following characters from forms. This is for security to prevent SQL injection, access to the command line, etc.
I'm sure there is a more efficient way of doing this by adding all the forbidden characters to an array but at the moment I have:
function userInput ($string) {

//Removing anything from the filename string that contains the following characters:
$string = str_replace(';', '', $string); //security
$string = str_replace('#', '', $string); //security
$string = str_replace('=', '', $string); //SQL injection
$string = str_replace('<', '', $string); //SQL injection
$string = str_replace('>', '', $string); //SQL injection
$string = str_replace('"', '', $string); //SQL injection
$string = str_replace('\'', '', $string); //SQL injection
$string = str_replace('%', '', $string); //SQL injection

return $string;

} //end function

Can I do this with just 1 str_replace line of code?
Thanks.

coopster

12:18 pm on Nov 17, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you read through the examples on the PHP manual page for the str_replace() [php.net] function? There is an example showing exactly how to do this with an array ;-)

jackvull

10:12 am on Nov 18, 2005 (gmt 0)

10+ Year Member



Works fine. What I also meant was is it quicker than doing a replace line by line?