How to escape strings in SQL Server using PHP?

前端 未结 14 1664
我寻月下人不归
我寻月下人不归 2020-11-22 09:36

I\'m looking for the alternative of mysql_real_escape_string() for SQL Server. Is addslashes() my best option or there is another alternative funct

14条回答
  •  旧巷少年郎
    2020-11-22 09:57

    It is better to also escape SQL reserved words. For example:

    function ms_escape_string($data) {
        if (!isset($data) or empty($data))
            return '';
    
        if (is_numeric($data))
            return $data;
    
        $non_displayables = array(
            '/%0[0-8bcef]/',        // URL encoded 00-08, 11, 12, 14, 15
            '/%1[0-9a-f]/',         // url encoded 16-31
            '/[\x00-\x08]/',        // 00-08
            '/\x0b/',               // 11
            '/\x0c/',               // 12
            '/[\x0e-\x1f]/',        // 14-31
            '/\27/'
        );
        foreach ($non_displayables as $regex)
            $data = preg_replace( $regex, '', $data);
        $reemplazar = array('"', "'", '=');
        $data = str_replace($reemplazar, "*", $data);
        return $data;
    }
    

提交回复
热议问题