Sign in to follow this  
Followers 0
myspacee

string replace (mysql_real_escape_string)

6 posts in this topic

Hello,

i use Autoit for insert informations into mysql db

Before that i use PHP but not fast as AI for Insert. Try to replicate a PHP function to text: mysql_real_escape_string

this PHP function is a stringreplace for these cases :

  • x00
  • n
  • r
  • '
  • "
  • x1a

 

anyone can post correct way to implement these text substitutions ?

Thank you!

m.

Share this post


Link to post
Share on other sites

?

$str = '\x00\n\r\"\x1a' & "'"

$str2 = StringRegExpReplace($str, '([\x00\\"\x1a\r\n''])', '\\$1')
msgbox(0,"", $str &@crlf& $str2)

Share this post


Link to post
Share on other sites

thank you mikell,

i hate compile StringRegExpReplace, I'm always afraid of making mistakes...

why you set "replace"= '$1'  ?

(StringRegExpReplace ( "test", "pattern", "replace", [count] )) ?

thank you again,

m.

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

$1 is the backreference to the capturing group, and the backslash must be escaped itself (special character)

$str = 'this is only a test'

$str2 = StringRegExpReplace($str, '(only|test)', '\\$1')
msgbox(0,"", $str &@crlf& $str2)
Edited by mikell

Share this post


Link to post
Share on other sites

Thank you for support,

so PHP function is not a simple text replacer, but it add escaping chars to given string...

so to emulate PHP function this is bad :

$str2 = StringRegExpReplace($str, '([\x00\\"\x1a\r\n''])', '')

and this is right way ?

$str2 = StringRegExpReplace($str, '([\x00\\"\x1a\r\n''])', '\\$1')

I'm sorry, but i need an additional clarification; i'm replacing an old PHP script with a custom tool,

and i must be sure to do it in best way possible.

thank you again for your time,

m.

Share this post


Link to post
Share on other sites

I'm not familiar with mysql but googling "mysql_real_escape_string alternative" I got this :

function mres($value)
{
    $search = array("\\",  "\x00", "\n",  "\r",  "'",  '"', "\x1a");
    $replace = array("\\\\","\\0","\\n", "\\r", "\'", '\"', "\\Z");

    return str_replace($search, $replace, $value);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0