标签 PHP 下的文章

PHP获取服务器的mac地址类

<?php
/**
 * 获取网卡的MAC地址,目前支持WIN/LINUX系统
 * 获取机器网卡的物理(MAC)地址
 */
class getMacAddr{
    var $return_array = array();
    var $mac_addr;

    function getMacAddr($os_type){
        switch (strtolower($os_type)) {
            case 'linux':
                $this->forLinux();
                break;
            case 'solaris':
                break;
            case 'unix':
                break;
            case 'aix':
                break;
            
            default:
                $this->forWindows();
                break;
        }
        $temp_array = array();
        foreach ($this->return_array as $value) {
            if(preg_match("/[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f]/i", $value, $temp_array)){
                $this->mac_addr = $temp_array[0];
                break;
            }
        }
        unset($temp_array);
        return $this->mac_addr;
    }

    function forWindows(){
        @exec("ipconfig /all", $this->return_array);
        if($this->return_array){
            return $this->return_array;
        }else{
            $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";
            if(is_file($ipconfig)){
                @exec($ipconfig." /all", $this->return_array);
            }else{
                @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->return_array);
            }
            return $this->return_array;
        }
    }

    function forLinux(){
        @exec("ifconfig -a", $this->return_array);
        return $this->return_array;
    }

}

//方法使用 
$mac = new getMacAddr(PHP_OS); 
echo $mac->mac_addr;

?>

cURL实现Get和Post请求的方法

private function https_request($url, $data = null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

PHP开发程序应该注意的42个优化准则

PHP 独特的语法混合了 C、Java、Perl 以及 PHP 自创新的语法。它可以比 CGI或者Perl更快速的执行动态网页。用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML文档中去执行,执行效率比完全生成 HTML标记的CGI要高许多。下面介绍了PHP开发程序应该注意的42个优化准则。

- 阅读剩余部分 -

PHP截取中文字符串编码

/**
 * 字符串截取,支持中文和其他编码
 * static 
 * access public
 * @param string $str 需要转换的字符串
 * @param string $start 开始位置
 * @param string $length 截取长度
 * @param string $charset 编码格式
 * @param string $suffix 截断显示字符
 * return string
 */
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true, $strip_tags=false){

    if($strip_tags){
        $str = strip_tags($str);//去除html标记
        $pattern = "/&[a-zA-Z]+;/";//去除特殊符号
        $str = preg_replace($pattern,'',$str);
    }

    if(function_exists("mb_substr")){
        $slice = mb_substr($str, $start, $length, $charset);
    }elseif(function_exists("iconv_substr")){
        $slice = iconv_substr($str, $start, $length, $charset);
        if(false === $slice){
            $slice = '';
        }
    }else{
        $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("", array_slice($match[0], $start, $length));
    }
    return $suffix ? $slice.'...' : $slice;
}

php日期数字转为汉字格式

$str = '2016-11-23';
function setDateCn($str){
    $month = str_split(date('m',strtotime($str)));
    $month = implode('十', $month);
    $month = str_replace('十0', '十', $month);
    $month = str_replace('1十', '十', $month);
    $month = str_replace('0十', '', $month);

    $day = str_split(date('d',strtotime($str)));
    $day = implode('十', $day);
    $day = str_replace('十0', '十', $day);
    $day = str_replace('1十', '十', $day);
    $day = str_replace('0十', '', $day);

    return str_replace(str_split('0123456789'), str_split('〇一二三四五六七八九',3), date('Y',strtotime($str)).'年'.$month.'月'.$day).'日'; 
}

php统计项目目录代码行数

<?php

/**
 * @author xiaoxia xu <x_824@sina.com> 2011-1-12
 * @link http://www.phpwind.com
 * @copyright Copyright &copy; 2003-2110 phpwind.com
 * @license
 * 统计目录下的文件行数及总文件数··去除注释
 */


$fileExt = ".php";
$filePath = "D:\install\wamp\www\qshx\Lawyer\Home";
if (!is_dir($filePath)) exit('Path error!');
list($totalnum, $linenum, $filenum) = readGiveDir($filePath, $fileExt);
echo "*************************************************************\r\n";
echo "总行数: " . $totalnum . "\r\n";
echo "总有效行数: " . $linenum . "\r\n";
echo '总文件个数:' . $filenum;


function readGiveDir($dir, $fileExt) {
    $totalnum = 0;
    $linenum = 0;
    $filenum = 0;
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if (in_array($file, array(".", "..", '.svn'))) continue;
            if (is_dir($dir . '/' . $file)) {
                list($num1, $num2, $num3) = readGiveDir($dir . '/' . $file, $fileExt);
                $totalnum += $num1;
                $linenum += $num2;
                $filenum += $num3;
            } else {
                if (strrchr($file, '.') == $fileExt) {
                    list($num1, $num2) = readfiles($dir . '/' . $file);
                    $totalnum += $num1;
                    $linenum += $num2;
                    $filenum ++;
                    continue;
                }
            }
        }
        closedir($dh);
    } else {
        echo 'open dir <' . $dir . '> error!' . "\r";
    }
    return array($totalnum, $linenum, $filenum);
}

function readfiles($file) {
    echo $file . "\r\n";
    //$p = php_strip_whitespace($file);
    $str = file($file);
    $linenum = 0;
    foreach ($str as $value) {
        if (skip(trim($value))) continue;
        $linenum ++;
    }
    $totalnum = count(file($file));
    echo '行数:' . $totalnum . "\r\n";
    echo '有效行数:' . $linenum . "\r\n";
    return array($totalnum, $linenum);
}

function skip($string) {
    if ($string == '') return true;
    $array = array("*", "/*", "//", "#");
    foreach ($array as $tag) {
        if (strpos($string, $tag) === 0) return true;
    }
    return false;
}

php单例模式

单例模式是一种常用的设计模式。在它的核心结构中只包含一个被称为单例类的特殊类。通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。

- 阅读剩余部分 -

PHP实现 数字转人民币金额大写

js将金额数字转换为繁体大写字

以下位PHP版转换代码:

function numToRmb($num) {
    $c1 = "零壹贰叁肆伍陆柒捌玖";
    $c2 = "分角元拾佰仟万拾佰仟亿";
    //精确到分后面就不要了,所以只留两个小数位
    $num = round($num, 2);
    //将数字转化为整数
    $num = $num * 100;
    if (strlen($num) > 10) {
        return "金额太大,请检查";
    }
    $i = 0;
    $c = "";
    while (1) {
        if ($i == 0) {
            //获取最后一位数字
            $n = substr($num, strlen($num) - 1, 1);
        } else {
            $n = $num % 10;
        }    //每次将最后一位数字转化为中文
        $p1 = substr($c1, 3 * $n, 3);
        $p2 = substr($c2, 3 * $i, 3);
        if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
            $c = $p1 . $p2 . $c;
        } else {
            $c = $p1 . $c;
        }
        $i = $i + 1;
        //去掉数字最后一位了
        $num = $num / 10;
        $num = (int) $num;
        //结束循环
        if ($num == 0) {
            break;
        }
    }
    $j = 0;
    $slen = strlen($c);
    while ($j < $slen) {
        //utf8一个汉字相当3个字符
        $m = substr($c, $j, 6);
        //处理数字中很多0的情况,每次循环去掉一个汉字“零”
        if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
            $left = substr($c, 0, $j);
            $right = substr($c, $j + 3);
            $c = $left . $right;
            $j = $j - 3;
            $slen = $slen - 3;
        }
        $j = $j + 3;
    }
    //这个是为了去掉类似23.0中最后一个“零”字
    if (substr($c, strlen($c) - 3, 3) == '零') {
        $c = substr($c, 0, strlen($c) - 3);
    }
    //将处理的汉字加上“整”
    if (empty($c)) {
        return "零元整";
    } else {
        return $c . "整";
    }
}