<?php

class StrUtil{

    /**
     * 判断字符串是否为 NULL 或者为 空白字符串  LiaoYun 2018-06-13
     * @param str
     * @return
     */
    public static function isEmpOrNull($str)
    {
        if ($str == null || trim($str) == "") {
            return true;
        } else {
            return false;
        }
    }

    public static function isNotEmpAndNull($str)
    {
        if ($str == null || trim($str) == "") {
            return false;
        } else {
            return true;
        }
    }

    public static function isMoblePhone(String $phone) {
    	if (self::isEmpOrNull($phone)) {
    		return false;
    	}
        if(!preg_match("/^1[3456789]\d{9}$/",$phone,$res)){
            return $res;
        } else {
            return false;
        }
    }

    public static function back(object $obj, string $funName)
    {

        $jsonStr = json_encode($obj);
        if (self::isNotEmpAndNull($funName)) {
            return "<script language='javascript'>parent.{$funName}({$jsonStr})</script>";
        } else {
            return "<script language='javascript'>parent.callback({$jsonStr})</script>";
        }

    }

    /**
     * 中文乱码转 utf-8, 对页面中 用 encodeURI(encodeURI(url)) 处理过的 url 有效
     * @return
     */
    public static function getRequestDecodeParam($request, $key)
    {
        $str = $request[$key];
        if ($str != null) {
            try {
                //解决%和+转码问题
                $str = preg_replace("/%(?![0-9a-fA-F]{2})/", "%25", $str);
                $str = preg_replace("/\\+/", "%2B", $str);
                return trim(urldecode($str));
            } catch (\Exception $e) {
                throw new \think\Exception($e->getMessage());
            }
        } else {
            return $str;
        }
    }

}