总结了一个公共类方法类:

class Util extends ArrayHelper
{
/**
* 判断是否为空数组
* @param mixed $arr
* @return boolean
*/
public static function isEmptyArray($arr)
{
if (empty($arr) || !is_array($arr) || count($arr) == 0) {
return true;
}
return false;
} /**
* 判断是否为空数据
* @param mixed $data
* @return boolean
*/
public static function isEmpty($data)
{
if (empty($data)) {
return true;
}
return false;
} // public static function isEmpty($value) {
// return $value === '' || $value === [] || $value === null || is_string($value) && trim($value) === '';
// } /**
* 解析时间范围查询条件
* @param string $strTimeRange
* @param string $inputType 字段配置中的input_type类型
* @param string $sep 间隔符
* @return array
*/
public static function parseTimeRange($strTimeRange, $inputType, $sep = '-')
{
$arrTimeRange = explode('-', $strTimeRange);
$arrTimeRange[0] = trim($arrTimeRange[0]);
$arrTimeRange[1] = trim($arrTimeRange[1]); if ('timestamp' === $inputType) {
if (!empty($arrTimeRange[0])) {
$arrTimeRange[0] = strtotime($arrTimeRange[0]);
} if (!empty($arrTimeRange[1])) {
$arrTimeRange[1] = strtotime($arrTimeRange[1]) + 86400 - 1;
}
} else {
if (!empty($arrTimeRange[1])) {
if (strpos($arrTimeRange[1], ':') === false) {
$arrTimeRange[1] .= ' 23:59:59';
}
}
}
return $arrTimeRange;
} /**
* 验证邮箱是否准确
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function checkEmail($str)
{
if (preg_match("/^([a-zA-Z0-9])+([\w-.])*([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)/", $str)) {
return true;
} else {
return false;
}
} /**
* 验证url格式
* @param string $url
* @return boolean
*/
public static function checkUrl($url)
{
if (!preg_match('/http:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is', $url)) {
return false;
}
return true;
} /**
* 验证是否全是中文
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function checkCn($str)
{
if (!eregi("[^\x80-\xff]", "$str")) {
return true;
} else {
return false;
}
} /**
* 判断是否存在中文
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function deCn($str)
{
$strlen = strlen($str);
$length = 1;
for ($i = 0; $i < $strlen; $i++) {
$tmpstr = ord(substr($str, $i, 1));
if (($tmpstr <= 161 || $tmpstr >= 247)) {
$a = 0;
} else {
$a = 1;
break;
}
}
if ($a == '0')
return false;
else
return true;
} /**
* 验证是否全是字母
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function checkEn($str)
{
if (preg_match("/^[a-zA-Z]+$/", "$str")) {
return true;
} else {
return false;
}
} /**
* 验证是否全是数字
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function checkNumber($str)
{
if (preg_match("/^[0-9]+$/", $str)) {
return true;
} else {
return false;
}
} /**
* 验证是否全是字符
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function checkStr($str)
{
if (preg_match("/^[a-zA-Z_0-9]+$/", "$str")) {
return true;
} else {
return false;
}
} /**
* 验证RGB color颜色
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function checkColorRGB($str)
{
if (strlen($str) != 6) {
return false;
}
if (preg_match("/^[a-zA-Z0-9]+$/", "$str")) {
return true;
} else {
return false;
}
} /**
* 验证是否全是英文字母或数字
* @param string $str 被验证的字符串
* @return boolean
*/
public static function checkLetterNumber($str)
{
if (preg_match("/^[a-zA-Z0-9]+$/", "$str")) {
return true;
} else {
return false;
}
} /**
* 检测微信id规则
* @param string $str
* @return boolean
*/
public static function checkWxId($str)
{
//前2位字符为"wx" 及 长度为16~20位(微信 appid长度为18位, 检测时考虑点伸缩)
if (substr($str, 0, 2) == 'wx' && preg_match("/^[a-zA-Z0-9]{16,20}$/", $str)) {
return true;
} else {
return false;
}
} /**
* 验证是否为unix时间戳
* @param string $str 被验证的字符串
* @return boolean
*/
public static function checkUnixTimeStamp($str)
{
if (preg_match("/^[1-9][0-9]{9}$/", "$str")) {
return true;
} else {
return false;
}
} /**
* 验证是否是百分率
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function checkPercent($str)
{
if (preg_match("/^[0-9]+(.[0-9]+)?%$/", "$str")) {
return true;
} else {
return false;
}
} /**
* 验证价格格式是否正确
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function checkMoney($str)
{
if (preg_match("/^[-]?[0-9]+(.[0-9]+)?$/", "$str")) {
return true;
} else {
return false;
}
} /**
* 验证浮点数
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function checkFloat($str)
{
if (preg_match("/^[0-9]+(.[0-9]+)?$/", "$str")) {
return true;
} else {
return false;
}
} /**
* 验证QQ号码
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function checkQq($str)
{
if (preg_match("/^[1-9][0-9]{4,9}$/", "$str")) {
return true;
} else {
return false;
}
} /**
* 验证手机号码
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function checkMobile($str)
{
if (preg_match("/^(?=\d{11}$)^1(?:3\d|4[57]|5[^4\D]|7[^249\D]|8\d)\d{8}$/", $str)) {
return true;
} else {
return false;
}
} /**
* 验证电话号码
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function checkPhone($str)
{
if (preg_match("/^([0]\d{2,3})?[1-9]{1}\d{6,7}$/", $str)) {
return true;
}
return false;
} /**
* 验证特殊电话号码, 如10000、95555、400号码(长度不小于5位的数字)
* @param string $str
* @return boolean
*/
public static function checkSpecialPhone($str)
{
if (preg_match("/^\d{5,20}$/", "$str")) {
return true;
} else {
return false;
}
} /**
* 验证身份证号码
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function checkIdCard($str)
{
if (preg_match("/^[1-9][0-9]{17}$/", "$str")) {
return true;
} else {
return false;
}
} /**
* 验证日期是否有效
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function checkDateTime($str)
{
if (preg_match("/^[1-9][0-9]{3}-[0-1]?[0-9]-[0-3][0-9]$/", "$str")) {
$tmp_arr = explode("-", $str);
// 『月 『日 『年
if (checkdate("$tmp_arr[1]", "$tmp_arr[2]", "$tmp_arr[0]")) {
return true;
}
} else {
return false;
}
} /**
* 验证ip地址
*
* @param string $str
* 被验证的字符串
* @return boolean
*/
public static function checkIp($str)
{
if (preg_match("/^[1-2]\d{1,2}\.\d{0,3}\.\d{0,3}\.\d{0,3}$/", $str)) {
return true;
} else {
return false;
}
} /**
* 判断用户输入的endpoint是否是 xxx.xxx.xxx.xxx:port 或者 xxx.xxx.xxx.xxx的ip格式
*
* @param string $endpoint 需要做判断的endpoint
* @return boolean
*/
public static function isIPFormat($endpoint)
{
$ip_array = explode(":", $endpoint);
$hostname = $ip_array[0];
$ret = filter_var($hostname, FILTER_VALIDATE_IP);
if (!$ret) {
return false;
} else {
return true;
}
} /**
* 生成query params
*
* @param array $options 关联数组
* @return string 返回诸如 key1=value1&key2=value2
*/
public static function toQueryString($options = array())
{
$temp = array();
uksort($options, 'strnatcasecmp');
foreach ($options as $key => $value) {
if (is_string($key) && !is_array($value)) {
$temp[] = rawurlencode($key) . '=' . rawurlencode($value);
}
}
return implode('&', $temp);
} /**
* 转义字符替换
*
* @param string $subject
* @return string
*/
public static function sReplace($subject)
{
$search = array('<', '>', '&', '\'', '"');
$replace = array('&lt;', '&gt;', '&amp;', '&apos;', '&quot;');
return str_replace($search, $replace, $subject);
} /**
* 检查是否是中文编码
*
* @param $str
* @return int
*/
public static function chkChinese($str)
{
return preg_match('/[\x80-\xff]./', $str);
} /**
* 检测是否GB2312编码
*
* @param string $str
* @return boolean false UTF-8编码 TRUE GB2312编码
*/
public static function isGb2312($str)
{
for ($i = 0; $i < strlen($str); $i++) {
$v = ord($str[$i]);
if ($v > 127) {
if (($v >= 228) && ($v <= 233)) {
if (($i + 2) >= (strlen($str) - 1)) return true; // not enough characters
$v1 = ord($str[$i + 1]);
$v2 = ord($str[$i + 2]);
if (($v1 >= 128) && ($v1 <= 191) && ($v2 >= 128) && ($v2 <= 191))
return false;
else
return true;
}
}
}
return false;
} /**
* 检测是否GBK编码
*
* @param string $str
* @param boolean $gbk
* @return boolean
*/
public static function checkChar($str, $gbk = true)
{
for ($i = 0; $i < strlen($str); $i++) {
$v = ord($str[$i]);
if ($v > 127) {
if (($v >= 228) && ($v <= 233)) {
if (($i + 2) >= (strlen($str) - 1)) return $gbk ? true : FALSE; // not enough characters
$v1 = ord($str[$i + 1]);
$v2 = ord($str[$i + 2]);
if ($gbk) {
return (($v1 >= 128) && ($v1 <= 191) && ($v2 >= 128) && ($v2 <= 191)) ? FALSE : TRUE;//GBK
} else {
return (($v1 >= 128) && ($v1 <= 191) && ($v2 >= 128) && ($v2 <= 191)) ? TRUE : FALSE;
}
}
}
}
return $gbk ? TRUE : FALSE;
} /**
* 判断字符串$str是不是以$findMe开始
*
* @param string $str
* @param string $findMe
* @return bool
*/
public static function startsWith($str, $findMe)
{
if (strpos($str, $findMe) === 0) {
return true;
} else {
return false;
}
} /**
* 检测是否windows系统,因为windows系统默认编码为GBK
*
* @return bool
*/
public static function isWin()
{
return strtoupper(substr(PHP_OS, 0, 3)) == "WIN";
} /**
* 主要是由于windows系统编码是gbk,遇到中文时候,如果不进行转换处理会出现找不到文件的问题
*
* @param $file_path
* @return string
*/
public static function encodePath($file_path)
{
if (self::chkChinese($file_path) && self::isWin()) {
$file_path = iconv('utf-8', 'gbk', $file_path);
}
return $file_path;
} /**
* The main function for converting to an XML document.
* Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
*
* @param array $data 待转换的数组
* @param string $rootNodeName 根结点名称
* - what you want the root node to be - defaultsto data.
* @param SimpleXMLElement $xml xml格式协议头
* - should only be used recursively
* @return string XML
*/
public static function toXml($data, $rootNodeName = 'data', $xml = null)
{
// turn off compatibility mode as simple xml throws a wobbly if you don't.
if (ini_get('zend.ze1_compatibility_mode') == 1) {
ini_set('zend.ze1_compatibility_mode', 0);
} if ($xml == null) {
$xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
} // loop through the data passed in.
foreach ($data as $key => $value) {
// no numeric keys in our xml please!
if (is_numeric($key)) {
// make string key...
$key = "unknownNode_" . (string)$key;
} // replace anything not alpha numeric
$key = preg_replace('/[^a-z]/i', '', $key); // if there is another array found recrusively call this function
if (is_array($value)) {
$node = $xml->addChild($key);
// recrusive call.
Util::toXml($value, $rootNodeName, $node);
} else {
// add single node.
$value = htmlentities($value);
$xml->addChild($key, $value);
}
}
// pass back as string. or simple xml object if you want!
return $xml->asXML();
} /**
* 将xml字符串转化成数组
*
* @param string $xml xml字符串
* @param boolean $flagCDATA 读取xml中<![CDATA[]]>数据
* @return array
*/
public static function xmlToArray($xml, $flagCDATA = false)
{
if (!$flagCDATA) {
$objTmp = simplexml_load_string($xml); //xml转化为对象
} else {
$objTmp = simplexml_load_string($xml, null, LIBXML_NOCDATA); //xml转化为对象
}
$strTmp = json_encode($objTmp); //对象转化为json
$arrTmp = json_decode($strTmp, true); //json转换为
return $arrTmp;
}
/**
* 号码隐藏
* @param $phone
* @return mixed
*/
public static function hideTelephone($phone)
{
if (!preg_match('/^\d{5,20}$/', $phone)) {
return $phone;
} $isWhat = preg_match('/(0[0-9]{2,3}[-]?[2-9][0-9]{6,7}[-]?[0-9]?)/i', $phone); //固定电话
if ($isWhat) {
return preg_replace('/(0[0-9]{2,3}[-]?[2-9])[0-9]{3,4}([0-9]{3}[-]?[0-9]?)/i', '$1****$2', $phone);
} else {
return preg_replace('/([1-9][0-9]{1}[0-9])[0-9]{1,4}([0-9]{1,4})/i', '$1****$2', $phone);
}
} /**
* 电话号码加密
* @param $phone
* @return string
*/
public static function encryptPhone($phone)
{
$head = 'HP:';
return $head . base64_encode($phone);
} /**
* 电话号码解密
* @param $phone
* @return string
*/
public static function decryptPhone($phone)
{
$phone = ltrim($phone, 'HP:');
return base64_decode($phone);
} static public function getRealHostIp()
{
$temp_ip = explode(':', $_SERVER['HTTP_HOST']);
$http_host = isset($temp_ip[0]) ? $temp_ip[0] : $_SERVER['SERVER_ADDR'];
return $http_host;
}
}

daicr工作中的总结:http://www.cnblogs.com/chrdai/p/8856448.html

php 公共方法Util的更多相关文章

  1. vue中添加util公共方法&&ES6之import、export

    vue中添加util公共方法&&ES6之import.export https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Re ...

  2. J2EE项目开发中常用到的公共方法

    在项目IDCM中涉及到多种工单,包括有:服务器|网络设备上下架工单.服务器|网络设备重启工单.服务器光纤网线更换工单.网络设备撤线布线工单.服务器|网络设备替换工单.服务器|网络设备RMA工单.通用原 ...

  3. Java中Excel导入功能实现、excel导入公共方法_POI -

    这是一个思路希望能帮助到大家:如果大家有更好的解决方法希望分享出来 公司导入是这样做的 每个到导入的地方 @Override public List<DataImportMessage> ...

  4. php 图片上传的公共方法(按图片宽高缩放或原图)

    写的用于图片上传的公共方法类调用方法: $upload_name='pic';$type = 'logo_val';$file_name = 'logo_' . $user_id .create_st ...

  5. web开发过程中经常用到的一些公共方法及操作

    进化成为程序猿也有段岁月了,所谓的经验,广度还是依旧,只不过是对于某种功能有了多种实现方式的想法.每天依旧不厌其烦的敲打着代码,每一行代码的回车似乎都有一种似曾相识的感觉.于是乎:粘贴复制,再粘贴再复 ...

  6. iOS常用公共方法

      iOS常用公共方法 字数2917 阅读3070 评论45 喜欢236 1. 获取磁盘总空间大小 //磁盘总空间 + (CGFloat)diskOfAllSizeMBytes{ CGFloat si ...

  7. Angularjs调用公共方法与共享数据

    这个问题场景是在使用ionic开发页面的过程中发现,多个页面对应的多个controller如何去调用公共方法,比如给ionic引入了toast插件,如何将这个插件的调用变成公共方法或者设置成工具类,因 ...

  8. [Guava官方文档翻译] 5. Guava的Object公共方法 (Common Object Utilities Explained)

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3537367.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  9. 单元测试时候使用[ClassInitialize]会该方法必须是静态的公共方法,不返回值并且应采用一个TestContext类型的参数报错的解决办法

    using Microsoft.VisualStudio.TestTools.UnitTesting; 如果该DLL应用的是 C:\Program Files\Microsoft Visual Stu ...

随机推荐

  1. Go中的panic和recover

    这两个内置函数,用来处理go的运行时错误. panic用来主动抛出错误, recover用来捕获panic抛出的错误. recover()和defer一起使用, 但是recover()只有在defer ...

  2. 一脸懵逼学习Hive的元数据库Mysql方式安装配置

    1:要想学习Hive必须将Hadoop启动起来,因为Hive本身没有自己的数据管理功能,全是依赖外部系统,包括分析也是依赖MapReduce: 2:七个节点跑HA集群模式的: 第一步:必须先将Zook ...

  3. snmp对超过16T的磁盘大小识别不对的解决办法

    https://blog.csdn.net/redleaf0000/article/details/38303299

  4. Windows下Mongodb启动问题

    把mongodb安装完,运行server出现问题

  5. Codeforces 311D Interval Cubing 数学 + 线段树 (看题解)

    Interval Cubing 这种数学题谁顶得住啊. 因为 (3 ^ 48) % (mod - 1)为 1 , 所以48个一个循环节, 用线段树直接维护. #include<bits/stdc ...

  6. Python图表数据可视化Seaborn:2. 分类数据可视化-分类散点图|分布图(箱型图|小提琴图|LV图表)|统计图(柱状图|折线图)

    1. 分类数据可视化 - 分类散点图 stripplot( ) / swarmplot( ) sns.stripplot(x="day",y="total_bill&qu ...

  7. 20165235 Java第一周学习总结

    (# 20165235 Java第一周学习总结 Ubuntu下git的安装与使用 首先Ubuntu下git的安装,使用sudo apt-get install git下载Ubuntu,下载完成后可以用 ...

  8. Floyd算法-傻子也能看懂的弗洛伊德算法(转)

                暑假,小哼准备去一些城市旅游.有些城市之间有公路,有些城市之间则没有,如下图.为了节省经费以及方便计划旅程,小哼希望在出发之前知道任意两个城市之前的最短路程.          ...

  9. 20165220 Java第四周学习总结

    教材学习内容总结 super:使用关键字super来访问和调用被子类隐藏的成员变量和方法. 接口:用关键字interface来定义一个接口.接口由类来实现以便使用接口中的方法,用关键字implemen ...

  10. 搭建本地maven库(nexus服务器)

    第一步,下载https://www.sonatype.com/download-oss-sonatype 别下3.x版本,下2.x版本 第二步,解压,在bin目录下执行cmd命令,nexus inst ...