分类 PHP 下的文章

tcpdf中函数使用

TCPDF 是一个流行的用于生成 PDF 文档的 PHP 类。TCPDF是当前唯一完整支持 UTF-8 Unicode 以及从右至左书写的语言包括双向文稿的 PHP 库。
TCPDF 是 SourceForge上最活跃的项目之一。
TCPDF 也是最多人使用的 PHP 库之一,因为最流行的一些基于 PHP 的 内容管理系统 中都带有TCPDF,包括: Joomla, Drupal, Moodle, phpMyAdmin, TCExam, Xoops, Elxis CMS, ImpressCMS, JELIX Frameweork PHP5, SugarCRM, Symfony, TYPO3, Vtiger CRM, Yii Framework, CMS Made Simple等。
TCPDF 类是源自于 公有领域 的 FPDF。FPDF 的开发者是 Olivier Plathey,但 TCPDF 已经几乎被重写,并且添加了数百个新的特性。.

- 阅读剩余部分 -

php获取两经纬度之间的距离

<?php 
/**
 * 获取指定经纬度之间距离
 *
 * 将地球假定为圆形,数据精准度有限
 * @param      integer  $lat1   纬度 1
 * @param      integer  $lng1   经度 1
 * @param      integer  $lat2   纬度 2
 * @param      integer  $lng2   经度 2
 *
 * @return     integer  指定位置距离 单位:米
 */
function get_distance($lat1, $lng1, $lat2, $lng2){
    $PI = 3.14159265358979323; // 圆周率
    $R = 6371229; // 地球的半径
    $x = $y = $distance = 0;

    $x = ($lng2 - $lng1) * $PI * $R * cos((($lat1 + $lat2) / 2) * $PI / 180) / 180;
    $y = ($lat2 - $lat1) * $PI * $R / 180;

    $distance = hypot($x, $y);
    return $distance;
}

// 北京到潍坊距离
echo get_distance(39.914889,116.403874,36.713212,119.168138);

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统计项目目录代码行数

<?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;
}