常用的common function库_mjcommonfuncs-程序员宅基地

技术标签: php  常用的common function库  

<?php
//因为前后台都要用到,所以放在supermario文件夹下方便重用
 
/**
 * 喔~可爱滴小数点~喔~可爱滴千位数~
 * 如果位数超过4位,则将第3位前增加,
 * @param int $gold
 * @return string
 */
function showGold($gold) {
    return number_format($gold);
}
 
/**
 * 页面压缩输出
 */
function ob_callback($buffer) {
    header('Etag: '.md5($buffer));
    if( extension_loaded('zlib') AND strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip") ) {
        $buffer = gzencode($buffer,9);
        header('Content-Encoding: gzip');
        header('Vary: Accept-Encoding');
        //header('Via: www.guangxitravel.cn');
        header('Content-Length: '.strlen($buffer));
    }
    return $buffer;
}
 
/**
 * 没有提示的情况下跳转页面
 * @param string $url
 * @param string $target
 */
function go($url='', $target='parent') {
    if ($url != "") {
        echo('<script language="javascript">window.location="' . $url . '";</script>');
    } else {
        echo('<script language="javascript">window.location=self.location;"</script>');
    }
}
 
/**
 * 清除缓存,ECHO,然后DIE(可恶的javascript+php……纯文本输出,总会有一些空字符……)
 * @param string $msg
 */
function justSay($msg) {
    ob_clean();
    die($msg);
}
 
/**
 * 将符合第二个参数的第一个参数里的内容替换为红色
 * @param string $data
 * @param array $keyword
 * @return string
 */
function change_keyword($data, $keywords) {
    if (is_array($keywords)) {
        foreach ($keywords as $k => $v) {
            $data = str_replace($v, '<span class="red b">' . $v . '</span>', $data);
        }
        return $data;
    } else {
        return str_replace($keywords, '<span class="red b">' . $keywords . '</span>', $data);
    }
}
 
/**
 * 创建完全随机的颜色
 * @return string
 */
function makeColor() {
    $key = '#';
    for ($i = 0; $i < 6; $i++)
        $key.= rand(0, 9);//生成php随机数
    return $key;
}
 
/**
 * 自动加载模型和第三方功能类
 * @param string $className
 */
function autoLoad($className) {
    if (strstr($_SERVER['REQUEST_URI'], "/operator/") == false) {   //服务器不支持SCRIPT_URL
        if (file_exists(siteRoot . '/client/model/' . $className . '.php')) {
            require_once siteRoot . '/client/model/' . $className . '.php';
            return;
        }
    } else {
        if (file_exists(siteRoot . '/manager/model/' . $className . '.php')) {
            require_once siteRoot . '/manager/model/' . $className . '.php';
            return;
        }
    }
    if (file_exists(siteRoot . '/public/modules/' . $className . '.php')) {
        require_once siteRoot . '/public/modules/' . $className . '.php';
        return;
    }
    if (file_exists(siteRoot . '/client/order/model/' . $className . '.php')) {
        require_once siteRoot . '/client/order/model/' . $className . '.php';
        return;
    }
}
 
/**
 * 用javascript弹出一条信息
 * @param <string> $message
 */
function message($message='') {
    echo('<script language="javascript">alert("' . $message . '");</script>');
}
 
/**
 * 模拟strstr()的第三个参数,返回$h中,$n之前的数据
 * //$h = haystack, $n = needle
 * @param <string> $h
 * @param <string> $n
 * @return <string>
 */
function strstrb($h, $n) {
    return array_shift(explode($n, $h, 2));
}
 
/**
 * 显示错误信息,并跳转至$pageurl,含有样式
 *
 * @param string $messages
 * @param string $pageurl
 * @param int $msc 秒数
 */
function msg($messages, $pageurl='javascript:history.back();', $msc=5) {
    echo '<title>System Message</title>
        <style type="text/css">
        *{margin:0;padding:0px}
        body{background:#fff;color:#333;font:12px Verdana, Tahoma, sans-serif;text-align:center;margin:0 auto;}
        a{text-decoration:none;color:#29458C}
        a:hover{text-decoration:underline;color:#f90}
        #msg{border:1px solid #c5d7ef;text-align:left;margin:10% auto; width:50%}
        #msgtitle{padding:5px 10px;background:#f0f6fb;border-bottom:1px #c5d7ef solid}
        #msgtitle h1{font-size:14px;font-weight:bold;padding-left:10px;border-left:3px solid #acb4be;color:#1f3a87}
        #msgcontent {padding:20px 50px;}
        #msgcontent li{display:block;padding:5px;list-style:none;}
        #msgcontent p{text-align:center;margin-top:10px;padding:0}
        </style>
        </head>
        <body>
        <div id="msg">
            <div id="msgtitle">
                <h1>Web System Message</h1>
            </div>
            <div id="msgcontent">
                ', $messages, '
                <p><a href="', $pageurl, '">>>>请点此处返回</a></p>
            </div>
        </div>
        <meta http-equiv="refresh" content="' . $msc . ';URL=', $pageurl, '">';
    exit();
}
 
/**
 * 检查后面的日期是否大于前面的日期
 * @param type $datecome 前面的日期
 * @param type $datego 后面的日期
 * @return type
 */
function sub_date($datecome, $datego) {
    $d1 = strtotime($datecome);
    $d2 = strtotime($datego);
    $days = round(($d2 - $d1) / 3600 / 24);
    $days = $days < 1 ? 0 : $days;
    return $days;
}
 
/**
 * 将$string的$length后边的内容用$dot替换
 * @param string $string
 * @param int $length
 * @param string $dot
 * @return string
 */
function cutstr($string, $length, $dot = '...') {
    @extract($string);
    if (strlen($string) <= $length) {
        return $string;
    }
    $string = htmlspecialchars($string);
    $string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $string);
    $strcut = '';
    $n = $tn = $noc = 0;
    while ($n < strlen($string)) {
        $t = ord($string[$n]);
        if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
            $tn = 1;
            $n++;
            $noc++;
        } elseif (194 <= $t && $t <= 223) {
            $tn = 2;
            $n += 2;
            $noc += 2;
        } elseif (224 <= $t && $t < 239) {
            $tn = 3;
            $n += 3;
            $noc += 2;
        } elseif (240 <= $t && $t <= 247) {
            $tn = 4;
            $n += 4;
            $noc += 2;
        } elseif (248 <= $t && $t <= 251) {
            $tn = 5;
            $n += 5;
            $noc += 2;
        } elseif ($t == 252 || $t == 253) {
            $tn = 6;
            $n += 6;
            $noc += 2;
        } else {
            $n++;
        }
        if ($noc >= $length) {
            break;
        }
    }
    if ($noc > $length) {
        $n -= $tn;
    }
    $strcut = substr($string, 0, $n);
    $strcut = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
    return $strcut . $dot;
}
 
/**
+----------------------------------------------------------
 * 如果 magic_quotes_gpc 为开启状态,则使用此方法使用为特殊符号前增加转移符号
+----------------------------------------------------------
 * @access public
+----------------------------------------------------------
 * @param string $value 可以为数组
+----------------------------------------------------------
 * @return string
+----------------------------------------------------------
 */
function kaddslashes($value) {
    return $value = is_array($value) ? array_map('kaddslashes', $value) : addslashes($value);
}
 
/**
+----------------------------------------------------------
 * 为特殊符号前去除转移符号
+----------------------------------------------------------
 * @access public
+----------------------------------------------------------
 * @param string $value 可以为数组
+----------------------------------------------------------
 * @return string
+----------------------------------------------------------
 */
function kstripcslashes($value) {
    return $value = is_array($value) ? array_map('kstripcslashes', $value) : stripcslashes($value);
}
 
/**
 * 将字符串内容html实体化,避免一些非法信息直接执行。如果参数是数组,则递归。
 * @param string $value
 * @return array
 */
function khtmlspecialchars($value) {
    return is_array($value) ? array_map('khtmlspecialchars', $value) :
            preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1', str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $value));
}
 
/**
 * 将字符串中的html去除,如果参数是数组,则递归。
 * @param string $value
 * @return array
 */
function striptags($value) {
    return $value = is_array($value) ? array_map('striptags', $value) : strip_tags($value);
}
 
/**
 * 检查email的合法性
 *
 * @param string $email
 * @return bool
 */
function check_email($email) {
    if (preg_match("/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i", $email)) {
        return true;
    } else {
        return false;
    }
}
 
/**
 * 安全电子邮件地址
 *
 * @param string $email
 * @param string $title
 * @param <type> $attributes
 * @return <type>
 */
function safe_mailto($email, $title = '', $attributes = '') {
    if (is_array($email)) {
        $tmp = $email;
        unset($email);
        extract($tmp);
    }
    $title = (string) $title;
 
    if ($title == "") {
        $title = $email;
    }
 
    for ($i = 0; $i < 16; $i++) {
        $x[] = substr('<a href="mailto:', $i, 1);
    }
 
    for ($i = 0; $i < strlen($email); $i++) {
        $x[] = "|" . ord(substr($email, $i, 1));
    }
 
    $x[] = '"';
 
    if ($attributes != '') {
        if (is_array($attributes)) {
            foreach ($attributes as $key => $val) {
                $x[] = ' ' . $key . '="';
                for ($i = 0; $i < strlen($val); $i++) {
                    $x[] = "|" . ord(substr($val, $i, 1));
                }
                $x[] = '"';
            }
        } else {
            for ($i = 0; $i < strlen($attributes); $i++) {
                $x[] = substr($attributes, $i, 1);
            }
        }
    }
 
    $x[] = '>';
 
    $temp = array();
    for ($i = 0; $i < strlen($title); $i++) {
        $ordinal = ord($title[$i]);
 
        if ($ordinal < 128) {
            $x[] = "|" . $ordinal;
        } else {
            if (count($temp) == 0) {
                $count = ($ordinal < 224) ? 2 : 3;
            }
 
            $temp[] = $ordinal;
            if (count($temp) == $count) {
                $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
                $x[] = "|" . $number;
                $count = 1;
                $temp = array();
            }
        }
    }
 
    $x[] = '<';
    $x[] = '/';
    $x[] = 'a';
    $x[] = '>';
 
    $x = array_reverse($x);
    ob_start();
    ?><script type="text/javascript">
    //<![CDATA[
    var l=new Array();
        <?php
            $i = 0;
        foreach ($x as $val) {
            ?>l[<?php echo $i++; ?>]='<?php echo $val; ?>';<?php } ?>
 
    for (var i = l.length-1; i >= 0; i=i-1){
        if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
        else document.write(unescape(l[i]));}
    //]]>
</script><?php
    $buffer = ob_get_contents();
    ob_end_clean();
    if ($tmp == '' || $tmp == NULL) {
        return $buffer;
    } else {
        echo $buffer;
    }
}
 
/**
+----------------------------------------------------------
 * 转换文字中的超链接为可点击连接
+----------------------------------------------------------
 * @access public
+----------------------------------------------------------
 * @param string $text 要处理的字符串
+----------------------------------------------------------
 * @return string
+----------------------------------------------------------
 */
function makeLink($string) {
    $validChars = "a-z0-9\/\-_+=.~!%@?#&;:$\|";
    $patterns = array(
        "/(^|[^]_a-z0-9-=\"'\/])([a-z]+?):\/\/([{$validChars}]+)/ei",
        "/(^|[^]_a-z0-9-=\"'\/])www\.([a-z0-9\-]+)\.([{$validChars}]+)/ei",
        "/(^|[^]_a-z0-9-=\"'\/])ftp\.([a-z0-9\-]+)\.([{$validChars}]+)/ei",
        "/(^|[^]_a-z0-9-=\"'\/:\.])([a-z0-9\-_\.]+?)@([{$validChars}]+)/ei");
    $replacements = array(
        "'\\1<a href=\"\\2://\\3\" title=\"\\2://\\3\" rel=\"external\">\\2://'.Input::truncate( '\\3' ).'</a>'",
        "'\\1<a href=\"http://www.\\2.\\3\" title=\"www.\\2.\\3\" rel=\"external\">'.Input::truncate( 'www.\\2.\\3' ).'</a>'",
        "'\\1<a href=\"ftp://ftp.\\2.\\3\" title=\"ftp.\\2.\\3\" rel=\"external\">'.Input::truncate( 'ftp.\\2.\\3' ).'</a>'",
        "'\\1<a href=\"mailto:\\2@\\3\" title=\"\\2@\\3\">'.Input::truncate( '\\2@\\3' ).'</a>'");
    return preg_replace($patterns, $replacements, $string);
}
 
/**
 * 获取客户端浏览器
 * @return string
 */
function browse_info() {
    $browser = "";
    $browserver = "";
    $Browsers = array("Lynx", "MOSAIC", "AOL", "Opera", "JAVA", "MacWeb", "WebExplorer", "OmniWeb");
    $Agent = $_SERVER["HTTP_USER_AGENT"];  //浏览器的全局变量
    for ($i = 0; $i <= 7; $i++) {
        if (strpos($Agent, $Browsers[$i])) {
            $browser = $Browsers[$i];
            $browserver = "";
        }
    }
    if (ereg("Mozilla", $Agent) && ereg("MSIE", $Agent)) {
        $temp = explode("(", $Agent);
        $Part = $temp[1];
        $temp = explode(";", $Part);
        $Part = $temp[1];
        $temp = explode(" ", $Part);
        $browserver = $temp[2];
        //$browserver =preg_replace("/([d.]+)/","1",$browserver);
        $browserver = "IE" . $browserver;
        $browser = "IE";
    }
    if (ereg("Mozilla", $Agent) && !ereg("MSIE", $Agent)) {
        $temp = explode("(", $Agent);
        $Part = $temp[0];
        $temp = explode("/", $Part);
        $browserver = $temp[1];
        $temp = explode(" ", $browserver);
        $browserver = $temp[0];
        $browserver = preg_replace("/([d.]+)/", "1", $browserver);
        $browserver = " $browserver";
        $browser = "Netscape Navigator";
    }
 
    if (ereg("Mozilla", $Agent) && ereg("Opera", $Agent)) {
        $temp = explode("(", $Agent);
        $Part = $temp[1];
        $temp = explode(")", $Part);
        $browserver = $temp[1];
        $temp = explode(" ", $browserver);
        $browserver = $temp[2];
        $browserver = preg_replace("/([\d\.]+)/", "1", $browserver);
        $browserver = " $browserver";
        $browser = "Opera";
    }
    //火狐浏览器
    if (ereg("Mozilla", $Agent) && ereg("Firefox", $Agent)) {
        $temp = explode("(", $Agent);
        $Part = $temp[1];
        $temp = explode(")", $Part);
        $browserver = $temp[1];
        $temp = explode(" ", $browserver);
        $browserver = $temp[2];
        $browser = "火狐";
    }
    //谷歌浏览器
    if (ereg("Mozilla", $Agent) && ereg("Chrome", $Agent)) {
        $temp = explode("(", $Agent);
        $temp = explode(" ", $temp[2]);
        $browserver = $temp['3']; //如果以后google升级不变位置的话就是它了
        $browser = "谷歌";
    }
    //360safe浏览器
    if (ereg("Mozilla", $Agent) && ereg("360SE", $Agent)) {
        //因为360浏览器,没有版本……
        $browserver = $browser = "360安全卫士";
    }
    if ($browser != "") {
        //$browseinfo = $browser.$browserver;
        $browseinfo = $browserver;
    } else {
        $browseinfo = "Unknown";
    }
    return $browseinfo;
}
 
/**
 * 返回时间格式
 * @name:  prepareDate
 * @desc:  prepares a date in the proper format for specific database types
 *         given a UNIX timestamp
 * @param: $timestamp: a UNIX timestamp
 * @param: $fieldType: the type of field to format the date for
 *         (in MySQL, you have DATE, TIME, YEAR, and DATETIME)
 */
function prepareDate($timestamp, $fieldType = 'DATETIME') {
    $date = '';
    if (!$timestamp === false && $timestamp > 0) {
        switch ($fieldType) {
            case 'DATE' :
                $date = date('Y-m-d', $timestamp);
                break;
            case 'TIME' :
                $date = date('H:i:s', $timestamp);
                break;
            case 'YEAR' :
                $date = date('Y', $timestamp);
                break;
            default :
                $date = date('Y-m-d H:i:s', $timestamp);
                break;
        }
    }
    return $date;
}
 
 
/**
 * 适应多维数组的递归,并将其中重复的值去掉后返回
 * @param array $array
 * @return array
 */
function super_unique($array) {
    $result = array_map("unserialize", array_unique(array_map("serialize", $array)));
 
    foreach ($result as $key => $value) {
        if (is_array($value)) {
            $result[$key] = super_unique($value);
        }
    }
 
    return $result;
}
 
/**
 * 如果数值不满足2位,自动补零
 * @param int $num
 * @return int
 */
function fullzero($num){
    if(strlen($num)!=2){
        return '0'.$num;
    }else{
        return $num;
    }
}
 
//返回上一个url
function get_visit_url(){
    return $_SERVER['HTTP_REFERER'];
}
?>

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wuxianbing2012/article/details/89327729

智能推荐

java方法的返回值储存到数组_java方法能返回数组么-程序员宅基地

文章浏览阅读1.3k次。这次要讲的是数组作为方法返回值的使用。一个方法可以有多个参数,可是只能有一个返回值,不可以有多个返回值。如果希望一个方法当中产生多个数据结果进行返回,那么就需要数组来作为返回值。数组作为方法返回值数组作为方法的返回值,返回的是数组的内存地址public static void main(String[] args) {//调用方法,接收数组的返回值//接收到的是数组的内存地址int[] arr2 ..._方法返回的值赋给数组元素

什么是CSDN_csdn是什么-程序员宅基地

文章浏览阅读760次。如题。是http://blog.csdn.net/Aaron25/category/183631.aspx把我吸引来的。谢谢他。 _csdn是什么

三方通话(二)_a=rtpmap:101 telephone-event-程序员宅基地

文章浏览阅读2k次。话机上三方通话建立过程如下消息:(1) 用户3315呼叫用户3305,终端发送INVITE消息给IPBX.INVITE sip:[email protected] SIP/2.0Via: SIP/2.0/UDP 192.168.50.119:31682;branch=z9hG4bK1791327125;rportFrom: "3315" ;tag=107903851_a=rtpmap:101 telephone-event

机器人学|手机玻璃加工全自动化——AGV+机器人+视觉解决方案(含双目三维视觉SLAM建图、MATLAB的AGV路径规划导航避障、六轴机械手臂建模与路径规划仿真,附带源代码)_agv路径规划与避障系统matlab-程序员宅基地

文章浏览阅读1k次,点赞22次,收藏30次。目标:某企业为3C部件精密加工企业,其加工的零件为手机玻璃,要求加工精度为±0.01mm,目前为人工运输至加工中心加工,由人工采用千分表在大理石平台上逐个测量实现。企业为减少人工成本,提高生产效率,要求采用自动化生产线方式实现。试调研国内外移动操作机器人现状,并作出自动化解决方案,列出主要部件BOM清单,并列出AGV+机器人+视觉形成的解决方案,列出技术点,并尝试计算采用自动化方案与采用人工方案相比,何时收回自动化生产线改造成本。_agv路径规划与避障系统matlab

【毕业设计教程】单片机发送短信消息(GMS模块) - 物联网 嵌入式 stm32_simcom 短信发送开发-程序员宅基地

文章浏览阅读7.2k次,点赞6次,收藏102次。Hi,大家好,这里是丹成学长,今天向大家介绍如何使用GMS模块,达到单片机发送短信的效果,应用场景非常广泛** 单片机发送短信消息(GMS模块)**大家可用于 课程设计 或 毕业设计单片机-嵌入式毕设选题大全及项目分享:https://blog.csdn.net/m0_71572576/article/details/125409052GSM模块使用上海SIMcom公司的SIM900高精度无线GSM/GPRS完全四频芯片,使用SMT封装且融 合了高性能的ARM926EJ-S内核。可以适应小型设备的高性价比_simcom 短信发送开发

Mybatis二级缓存的缺陷_mybatis二级缓存的缺点-程序员宅基地

文章浏览阅读1w次,点赞7次,收藏17次。Mybatis二级缓存的缺陷_mybatis二级缓存的缺点

随便推点

java中的宏变量,宏定义中的特殊参数(#、##、...和__VA_ARGS__)-程序员宅基地

文章浏览阅读257次。##名称:预处理拼接符,或者称其为宏拼接符作用:用于类似函数的宏的替换部分,还可以用于类似对象的宏的替换部分。##放在宏的替换部分的前面,用于宏展开(即宏替换)后,立即将宏中位于##右边的宏替换部分与该宏中位于##左边的部分相拼接至一个整体。例子1:// 宏定义#define XNAME(n) x##n // 宏调用 int XNAME(4) = 1; // 宏展开(即宏替换)后,我们得到: in..._java 解析字符串中的宏变量

C汇编语言是符号化的机器语言,汇编语言-程序员宅基地

文章浏览阅读3.4k次。汇编语言(Assembler Language)[编辑]什么是汇编语言汇编语言是指能反映指令功能的助记符表达的计算机语言,它是符号化的机器语言。用汇编语言写出的程序是汇编语言源程序,机器无法执行。必须用计算机配置好的汇编程序把它翻译成机器语言目标程序,机器才能执行。这个翻译过程就是汇编过程。汇编语言比机器语言在编写、修改、阅读方面均有很大改进,运行速度也快,但掌握起来比较困难。[编辑]汇编语言的特..._汇编语言是符号化的机器语言

python调用c动态库函数_python调用C/C++动态库-程序员宅基地

文章浏览阅读803次。本文以windows环境下的.dll动态链接库为背景展开,有关linux下的.so动态链接库的相关用法会在另外一篇文章中展开讲解。1. 背景知识一直以来python都被称为胶水语言,能够轻易地操作其他程序,轻易地包装使用其他语言编写的库。下面简单介绍下如何使用python来调用C/C++编写的动态库。首先了解下动态链接库及C/C++动态库的区别。1.1 动态链接库使用VS2017创建动态链接库Dl..._python调用c语言动态库函数有unsigned char*inbuf参数类型

pads单位切换的快捷键_PADS快捷键和技巧大全-程序员宅基地

文章浏览阅读1.9k次,点赞2次,收藏7次。发一些最基本常用的几个快捷键:设置通孔显示模式:D+O设置铜只显示外框形式:P+O改变当前层:L(如改当前层为第二层,为L2)测量:从当前位置开始测量:Q改变线宽:W设置栅格:G对找元件管脚或元件:S寻找绝对坐标点:S(n)(n)改变走线角度:AA任意角,AD斜角,AO直角取消当前操作:UN,如UN(1)为取消前一个操作重复多次操作:RE设计规则检查:打开: DRP,关闭:DRO,忽略设计规则:D..._pads单位切换快捷键

React Native开发坑之null is not an object (evaluating 'inst.componentWillReceiveProps')_react native nativeevent 为什么是null-程序员宅基地

文章浏览阅读5k次。最近写react native发现一个坑,出现这个错误:null is not an object (evaluating 'inst.componentWillReceiveProps')坑的是完全没有提示是哪个类出错了。后面发现在render函数里面使用了//进行注释代码~真的是醉了,JSX中是不能使用//做注释,如果一定_react native nativeevent 为什么是null

力扣java刷题学习(代码随想录学习)2_代码随想录java版本代码-程序员宅基地

文章浏览阅读405次。【代码】力扣java刷题学习(代码随想录学习)2。_代码随想录java版本代码