php代码片段

php库

PHP 开发者应了解的 24 个库

获取文件扩展名

// 获取文件扩展名
functionget_extension($file)
{
    return pathinfo($file, PATHINFO_EXTENSION);
}
  • PATHINFO_DIRNAME - 目录
  • PATHINFO_BASENAME - 文件名(含扩展名)
  • PATHINFO_EXTENSION - 扩展名
  • PATHINFO_FILENAME - 文件名(不含扩展名,PHP>5.2)

这四个常量的值分别是1、2、4、8,刚开始我还以为可以通过或运算指定多个:
pathinfo($file, PATHINFO_EXTENSION | PATHINFO_FILENAME);
后来发现这样不行,这只会返回几个进行或运算常量中最小的那个。也就是四个标志位中最小位为1的常量。

object 转 array数组形式 (有些 object 一层 array 一层 object 的。。。)

function std_class_object_to_array($stdclassobject)
{
    $_array = is_object($stdclassobject) ? get_object_vars($stdclassobject) : $stdclassobject;

    foreach ($_array as $key => $value) {
        $value = (is_array($value) || is_object($value)) ? std_class_object_to_array($value) : $value;
        $array[$key] = $value;
    }

    return $array;
}

php输出utf8格式文件

$f=fopen("test.txt", "wb");
$text=utf8_encode("a!");
//先用函数utf8_encode将所需写入的数据变成UTF编码格式。
$text="\xEF\xBB\xBF".$text;
//"\xEF\xBB\xBF",这串字符不可缺少,生成的文件将成为UTF-8格式,否则依然是ANSI格式。
fputs($f, $text);
//写入。
fclose($f);

Exception 示例

function func_try_except(){
    try{
        if(true)
            throw new Exception('catch me!');
    }catch(Exception $e){
        echo $e;
    }
}

简单方便的分页类

/**
*@param $count 总条数
*@param $perpage 每页显示条数
*@param $curpage 当前页
*@param $mpurl 页面链接
*@return 分页 html (输出形式 first<< 123 >>last)
*/
class XZ_paging{
    public static function pagination($count, $perpage, $curpage, $mpurl, $maxpages = 0, $page = 10){

        $realpages = 1;
        $page -= strlen($curpage) - 1;
        if($page <= 0) {
            $page = 1;
        }

        $multipage = "<div class='pagination'><ul>";

        if($count > $perpage) {

            $offset = floor($page * 0.5);

            $realpages = @ceil($count / $perpage);
            $curpage = $curpage > $realpages ? $realpages : $curpage;
            $pages = $maxpages && $maxpages < $realpages ? $maxpages : $realpages;

            if($page > $pages) {
                $from = 1;
                $to = $pages;
            } else {
                $from = $curpage - $offset;
                $to = $from + $page - 1;
                if($from < 1) {
                    $to = $curpage + 1 - $from;
                    $from = 1;
                    if($to - $from < $page) {
                        $to = $page;
                    }
                } elseif($to > $pages) {
                    $from = $pages - $page + 1;
                    $to = $pages;
                }
            }

            $multipage .= "<li><a ref='' href='{$mpurl}&page={$from}'>first<< </a></li>";
            for($i = $from; $i <= $to; $i++) {
                $multipage .= $i == $curpage ? "<li class='active' ><span ref='' href='{$mpurl}&page={$i}'><strong>{$i}</strong></span></li>" : "<li><a ref='' href='{$mpurl}&page={$i}'>{$i}</a></li>";
            }
            $multipage .= "<li><a ref='' href='{$mpurl}&page={$realpages}'> >>last</a></li>";
        }else{
            $multipage .= "<li><a ref='' href='{$mpurl}&page=1'>first<< </a></li>";
            $multipage .= "<li class='active' ><span ref='' href='{$mpurl}&page=1'><strong>1</strong></span></li>";
            $multipage .= "<li><a ref='' href='{$mpurl}&page=1'> >>last</a></li>";
        }

        return $multipage .= "</ul></div>";
    }
}

你可能感兴趣的:(PHP)