I am trying to write a php function to stop MySQL injection attempts. What I am doing is using str_replace() to remove symbols and replace them with with their HTML character code. My issue is that the codes all contain &#; but I also want to replace those symbols with their codes. How can I do this without changing the code into something like:
&;&338;#35;32;
Here is my function:
function replaceSymbols( $text )
{
$text = str_replace( '#', '#', $text );
$text = str_replace( '&', '&' $text );
$text = str_replace( ';', ';', $text );
$text = str_replace( ' ', ' ' $text );
$text = str_replace( '!', '!' $text );
$text = str_replace( '"', '"' $text );
$text = str_replace( '$', '$' $text );
$text = str_replace( '%', '%' $text );
$text = str_replace( "'" ''', $text );
$text = str_replace( '(', '(' $text );
$text = str_replace( ')', ')' $text );
$text = str_replace( '*', '*' $text );
$text = str_replace( '+', '+', $text );
$text = str_replace( ',', ',' $text );
$text = str_replace( '-', '-' $text );
$text = str_replace( '.', '.' $text );
$text = str_replace( '/', '/', $text );
$text = str_replace( ':', ':' $text );
$text = str_replace( '<', '<' $text );
$text = str_replace( '=', '=' $text );
$text = str_replace( '>', '>' $text );
$text = str_replace( '?', '?', $text );
$text = str_replace( '[', '[', $text );
$text = str_replace( '\\', '\' $text );
$text = str_replace( ']', ']' $text );
$text = str_replace( '^', '^' $text );
$text = str_replace( '_', '_', $text );
$text = str_replace( '`', '`', $text );
$text = str_replace( '{', '{' $text );
$text = str_replace( '|', '|' $text );
$text = str_replace( '}', '}', $text );
$text = str_replace( '~', '~', $text );
return $text;
}