<?php
$debug = 0;
$debug_a = 0;
if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
        
        //$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
        
        switch ($theType) {
            case "text":
                $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                break;
            case "long":
                break;
            case "int":
                $theValue = ($theValue != "") ? intval($theValue) : "NULL";
                break;
            case "double":
                $theValue = ($theValue != "") ?  doubleval($theValue) : "NULL";
                break;
            case "date":
                $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                break;
            case "defined":
                $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
                break;
        }
        return $theValue;
    }
}
//模拟提交的变量对其进行转码
function GetSQLValueStringGBK($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
    $theValue = mb_convert_encoding($theValue, "GBK", "UTF-8");//(iconv('UTF-8', 'GBK', $theValue));
    return GetSQLValueString($theValue, $theType);
}
//模拟提交的变量对其进行转码 2018-07
function save2mssql($theValue)
{
    return mb_convert_encoding($theValue, "GBK", "UTF-8");//return iconv('UTF-8', 'GBK', $theValue);
}
//对查询后的结果进行转码 2018-07
function loadFromMssql($theValue)
{
    return mb_convert_encoding($theValue, "UTF-8", "GBK");//('GBK', 'UTF-8', $theValue);
}

 
function generate_password($length = 8)
{
    // 密码字符集，可任意添加你需要的字符  
    $chars    = 'ABCD1234567890';
    $password = '';
    for ($i = 0; $i < $length; $i++) {
        // 这里提供两种字符获取方式  
        // 第一种是使用 substr 截取$chars中的任意一位字符；  
        // 第二种是取字符数组 $chars 的任意元素  
        // $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);  
        $password .= $chars[mt_rand(0, strlen($chars) - 1)];
    }
    return $password;
}


function console_log($data)
{
    if (is_array($data) || is_object($data)) {
        echo ("<script>console.log(' array  " . json_encode($data) . "');</script>");
    } else {
		$data=str_replace("'", "\'",$data);
        echo ("<script>console.log('" . str_replace(PHP_EOL, '', $data) . "');</script>");
    }
}
/**
 * Determine if a variable is iterable. i.e. can be used to loop over.
 *
 * @return bool
 */
 if (!function_exists("is_iterable")) {
	function is_iterable($var)
	{
		return $var !== null 
			&& (is_array($var) 
				|| $var instanceof Traversable 
				|| $var instanceof Iterator 
				|| $var instanceof IteratorAggregate
				);
	}
}
function str_pad_chn($input , $pad_length ,$pad_string , $pad_type){
            $strlen =(strlen($string) + mb_strlen($string,'UTF8')) / 2;
            if($strlen < $pad_length){
                $difference = $pad_length - $strlen;
                switch ($pad_type) {
                    case STR_PAD_RIGHT:
                        return $input . str_repeat($pad_string, $difference);
                        break;
                    case STR_PAD_LEFT:
                        return str_repeat($pad_string, $difference) . $input;
                        break;
                    default:
                        $left = $difference / 2;
                        $right = $difference - $left;
                        return str_repeat($pad_string, $left) . $input . str_repeat($pad_string, $right);
                        break;
                }
            }else{
                return $input;
            }
 }
?>