Skip to content

Instantly share code, notes, and snippets.

@wilnaweb
Last active February 4, 2023 02:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilnaweb/ca37459bdf92aa74bd2dd7994fa5b0df to your computer and use it in GitHub Desktop.
Save wilnaweb/ca37459bdf92aa74bd2dd7994fa5b0df to your computer and use it in GitHub Desktop.
Alternative mysql_real_escape_string without mysql connection
#Function
#Refer: https://www.php.net/manual/pt_BR/function.mysql-real-escape-string.php#101248
function escape_string($param) {
if(is_array($param))
return array_map(__METHOD__, $param);
if(!empty($param) && is_string($param)) {
return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $param);
}
return $param;
}
#Using example
$name = @trim(escape_string(stripslashes($_POST['name'])));
@Luc45
Copy link

Luc45 commented Jun 18, 2021

@wilnaweb
Copy link
Author

The functions are similar, the difference is in the more complete character map in the recommended function.

%?

Better to use https://github.com/abreksa4/mysql-escape-string-polyfill/blob/master/src/functions.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment