php小功能集合

1.时间处理

刚刚 一天前 一个月前

public function timeCode($param)
{
    $now_time = time();
    if ($now_time - $param < 60 * 3) {
        $putTime = '刚刚';
    } else if ($now_time - $param < 60 * 60) {
        $putTime = ceil(($now_time - $param) / 60) . '分钟前';
    } else if ($now_time - $param < 60 * 60 * 24) {
        $putTime = ceil(($now_time - $param) / 60 / 60) . '小时前';
    } else if ($now_time - $param < 60 * 60 * 24 * 30) {
        $putTime = ceil(($now_time - $param) / 60 / 60 / 24) . '天前';
    } else {
        $putTime = ceil(($now_time - $param) / 60 / 60 / 24 / 30) . '月前';
        //$putTime = date('Y-m-d h:i:s', $param);
    }

    return $putTime;
}

2.敏感词判断和替换

/**
 * 
 * @param type $param
 * @return type  敏感词替换
 */
public function FilterWordReplace($param = '')
{
    $DisRedis = new \KIF\Cache\DisRedis();
    $senWord  = $DisRedis->get('senstive_word');
    $word     = preg_replace('/' . $senWord . '/i', "**", $param);
    return $word;
}

/**
 * 
 * @param type $pame
 * @return string
 * 敏感词判断
 */
public function doFilterWord($pame = '')
{
    $DisRedis = new \KIF\Cache\DisRedis();
    $senWord  = $DisRedis->get('senstive_word');

    $isWord = preg_match('/' . $senWord . '/i', $pame, $matchs);

    if ($isWord) {
        return $matchs;
    } else {
        return '';
    }
}

3.php转换\n 为html

nl2br();

4.获取文章的摘要

/**
 *  获取文章的摘要 
 *  
 * @param string $data
 * @param int $cut 字数 自定义
 * @param string $str
 * @return type
 */
public function cutArticle($data, $cut = 200, $str = "...")
{
    $data    = strip_tags($data);
    $pattern = "/&[a-zA-Z]+;/";
    $data    = preg_replace($pattern, '', $data);
    if (!is_numeric($cut)) {
        return $data;
    }
    if ($cut > 0) {
        $data = trim(mb_strimwidth($data, 0, $cut, $str));
    }
    return $data;
}

5.获取联想词

public function helper()
{
    $keyword = I('get.keyword', '', 'htmltotxt');
    $data    = file_get_contents("http://suggestion.baidu.com/su?wd={$keyword}&cb=");
    // $data =ltrim($data, "(");
    // $data =rtrim($data, ");");
    $data = iconv('GB2312', 'UTF-8', $data);
    //正则匹配所有中文
    $list   = preg_match_all("#(?:(?![,。?])[\xC0-\xFF][\x80-\xBF]+)+#", $data, $arr, PREG_PATTERN_ORDER);
    $tmpArr = $arr['0'];
    //去除重复
    $uniqueArr = array_unique($tmpArr);
    die(Json::minijson($uniqueArr));

}

6.大数据excel导出

/**
 * 导出数据 fput
 * @param type $param
 */
public static function exportCsvFile($file_name, $tArr = [], $dataArr = [], $num = 0)
{
    $file_name = iconv('utf-8', 'gb2312', $file_name);
    if (file_exists($file_name) && $num == 1) {
        unlink($file_name);
    }
    if ($num == 1) {
        $dataStr .= implode(',', $tArr);
        $dataStr .= "\n"; //用引文逗号分开
    }
    foreach ($dataArr as $key => $value) {
        $dataStr .= implode(',', $value);
        $dataStr .= "\n"; //用引文逗号分开
    }
    $dataStr = iconv('utf-8', 'gb2312', $dataStr);
    ob_flush();
    flush();
    file_put_contents($file_name, $dataStr, FILE_APPEND);
}

7.页面导出excel数据

/**
 * 导出数据
 * 在浏览器输出并下载
 */
public static function exportExcelHtml($title, $tArr = [], $dataArr = [])
{
    if ($tArr && $dataArr) {
        //文件名
        $filename = $title . ".xls";

        //生成HTML
        $htmlStr .= "";
        //生成头部
        $htmlStr .= "";
        foreach ($tArr as $value) {
            $htmlStr .= "";
        }
        $htmlStr .= "";

        //生成数据
        foreach ($dataArr as $key => $rt) {
            $htmlStr .= "";
            foreach ($rt as $k => $v) {
                $htmlStr .= "";
            }
            $htmlStr .= "\n";
        }

        $htmlStr .= "
{$value}
{$v}
"; //输出数据流 header("Content-Type: application/vnd.ms-excel;charset=utf-8"); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=" . $filename); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Pragma: no-cache"); header("Expires: 0"); exit($htmlStr); } }

8.使用phpexcel类导入导出excel

/**
 * 导入数据
 * 使用phpexcel类
 */
public function importExcel($filePath, $exts = 'xls')
{
    //如果excel文件后缀名为.xls,导入这个类
    if ($exts == 'xls') {
        Vendor('PHPExcel.PHPExcel.Reader.Excel5');
        $PHPReader = new \PHPExcel_Reader_Excel5();
    } elseif ($exts == 'xlsx') {
        Vendor('PHPExcel.PHPExcel.Reader.Excel2007');
        $PHPReader = new\PHPExcel_Reader_Excel2007();
    }
    //载入excel数据解析
    $PHPExcel           = $PHPReader->load($filePath);
    //初始化
    $objWorksheet       = $PHPExcel->getSheet(0);
    // 取得总行数  
    $highestRow         = $objWorksheet->getHighestRow();
    // 取得总列数  
    $highestColumn      = $objWorksheet->getHighestColumn();
    $highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);

    $excelData = array();
    for ($row = 2; $row <= $highestRow; $row++) {
        for ($col = 0; $col < $highestColumnIndex; $col++) {
            $excelData[$row - 1][] = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
        }
    }
    return $excelData;
}

/**
 * 导出数据
 * 使用phpexcel类
 */
public function exportExcel($title, $tArr = [], $dataArr = [])
{
    //引入第三方excel类库
    Vendor('PHPExcel.PHPExcel');
    $objPHPExcel = new \PHPExcel();
    $objPHPExcel->setActiveSheetIndex(0);
    $dataArray   = [$tArr];
    foreach ($dataArr as $key => $value) {
        $dataArray[] = $value;
    }
    $objPHPExcel->getActiveSheet()->fromArray($dataArray, null, 'A1');
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="' . $title . xls . '"');
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output');
    $objPHPExcel->disconnectWorksheets();
}

你可能感兴趣的:(php小功能集合)