PHP小函数集-篇一

一、 验证

 

 

    /**
     * 判断用户名是否规范
     */
    function is_username($username)
    {
        if (preg_match("/^[a-zA-Z]{1}([0-9a-zA-Z]|[._]){3,19}$/",$username))
        {
            return true;
        }
    }

 

    /**
     * 判断密码是否规范
     */
    function is_password($password)
    {
        if (preg_match("/^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,22}$/",$password))
        {
            return true;
        }
    }

 

    /**
     * 判断是否为数字
     */
    function is_number($number)
    {
        if (preg_match("/^[0-9]*[1-9][0-9]*$/", $number))
        {
            return true;
        }
    }

 

    /**
     * 判断是否为邮件地址
     */
    function is_email($email)
    {
        if (preg_match("/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/", $email))
        {
            return true;
        }
    }

 

    /**
     * 判断是否为字母
     */
    function is_letter($letter)
    {
        if (preg_match("/^[a-z]+$/", $letter))
        {
            return true;
        }
    }

 

    /**
     * 验证文本域输入的内容
     */
    function is_text($text)
    {
        if (preg_match("/^[\x{4e00}-\x{9fa5}a-zA-Z0-9,,。.??!!*-_\s]+$/u", $text))
        {
            return true;
        }
    }

 

    /**
     * 验证文本框输入的内容
     */
    function is_textarea($textarea)
    {
        if (preg_match("/^[\x{4e00}-\x{9fa5}a-zA-Z0-9,,。.??!!*-_@=#\s\n\r]+$/u", $textarea))
        {
            return true;
        }
    }

 

 

    /**
     * 判断 文件/目录 是否可写
     */
    function check_writeable($file)
    {
        if (file_exists($file))
        { 
            if (is_dir($file))
            {
                $dir = $file;
                if ($fp = @fopen("$dir/test.txt", 'w'))
                {
                    @fclose($fp);
                    @unlink("$dir/test.txt");
                    $writeable = 1;
                }
                else
                {
                    $writeable = 0;
                }
            }
            else
            {
                if ($fp = @fopen($file, 'a+'))
                {
                    @fclose($fp);
                    $writeable = 1;
                }
                else
                {
                    $writeable = 0;
                }
            }
        }
        else
        {
            $writeable = 2;
        }
    
        return $writeable;
    }

 

    /**
     +----------------------------------------------------------
     * 递归方式的对变量中的特殊字符进行转义
     +----------------------------------------------------------
     */
    function addslashes_deep($value)
    {
        if (empty ($value))
        {
            return $value;
        }

        if (is_array($value))
        {
            foreach ((array) $value as $k => $v)
            {
                unset ($value[$k]);
                $k = addslashes($k);
                if (is_array($v))
                    $value[$k] = addslashes_deep($v);
                else
                    $value[$k] = addslashes($v);
            }
        }
        else
        {
            $value = addslashes($value);
        }
        return $value;
    }

    /**
     +----------------------------------------------------------
     * 递归方式的对变量中的特殊字符去除转义
     +----------------------------------------------------------
     */
    function stripslashes_deep($value)
    {
        if (empty ($value))
        {
            return $value;
        }

        if (is_array($value))
        {
            foreach ((array) $value as $k => $v)
            {
                unset ($value[$k]);
                $k = stripslashes($k);
                if (is_array($v))
                {
                    $value[$k] = stripslashes_deep($v);
                }
                else
                {
                    $value[$k] = stripslashes($v);
                }
            }
        }
        else
        {
            $value = stripslashes($value);
        }
        return $value;
    }

    /**
     +----------------------------------------------------------
     * 交互数据转义操作
     +----------------------------------------------------------
     */
    function dou_magic_quotes()
    {
        if (!@ get_magic_quotes_gpc())
        {
            if (!empty ($_GET))
                $_GET = addslashes_deep($_GET);
            if (!empty ($_POST))
                $_POST = addslashes_deep($_POST);
            $_COOKIE = addslashes_deep($_COOKIE);
            $_REQUEST = addslashes_deep($_REQUEST);
        }
    }

 

    /**
     +----------------------------------------------------------
     * 获取真实IP地址
     +----------------------------------------------------------
     */
    function get_ip()
    {
        $ip = false;
        if (!empty ($_SERVER["HTTP_CLIENT_IP"]))
        {
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }
        if (!empty ($_SERVER['HTTP_X_FORWARDED_FOR']))
        {
            $ips = explode(", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
            if ($ip)
            {
                array_unshift($ips, $ip);
                $ip = FALSE;
            }
            for ($i = 0; $i < count($ips); $i++)
            {
                if (!preg_match("/^(10|172\.16|192\.168)\./", $ips[$i]))
                {
                    $ip = $ips[$i];
                    break;
                }
            }
        }
        return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
    }

 

 

 

 

 

posted @ 2014-06-11 11:27  oaijuru  阅读(198)  评论(0编辑  收藏  举报