The reason MySQL functions require a connection in order to avoid a string is because all mysql_real_escape_string() makes a call to MySQL - in the escaping function.
However, if you read the manual page for it , you will see that they list the characters that are escaped:
mysql_real_escape_string () calls the MySQL library function mysql_real_escape_string, which adds a backslash to the following characters: \ x00, \ n, \ r, \, ', "and \ x1a.
You do not want to use addslashes() , as this only eludes several characters and does not provide a safe solution. But you should be able to mysql_real_escape_string() escaping performed by mysql_real_escape_string() using a list of characters, with a simple call to strtr() or similar:
$replacements = array("\x00"=>'\x00', "\n"=>'\n', "\r"=>'\r', "\\"=>'\\\\', "'"=>"\'", '"'=>'\"', "\x1a"=>'\x1a'); $escaped = strtr($unescaped,$replacements);
source share