将数组拼接为 table 标签字符串

<?php
//判断是否是图片
function isImage($string){
$isImage = false;
$pics = ['.png', '.jpg', '.gif'];
foreach ($pics as $pic){
if(strpos($string, $pic) != false){
$isImage = true;
}
}
return $isImage;
}
/**
* 将数组输出为table
* @param $arr
* 数组
* @param $len
* 每行的列数
* @return string
* 返回拼装的标签字符串
*/
function printTable($arr, $len){
$result = '';
// 记录当前是第几列
$i = 0;
if(!empty($arr)){
$result .= '<table>';
foreach ($arr as $valueKey => $valueInfo) {
// 行开头加开头标签 tr
if($i == 0){
$result .= '<tr>';
}
// 输出每行数据 td
$result .= '<td>';
if(isImage($valueInfo)){
$result .= '<img style="width: 120px;" src='.$valueInfo.' />';
}else{
$result .= '<p>'.$valueInfo.'</p>';
}
$result .= '</td>';
// 行结尾
if($i == $len - 1){
$result .= '</tr>';
$i = 0;
continue;
}
$i++;
}
$result .= '</table>';
}
return $result;
}
?>

下载文件

/**
* 将文件转为流,输出到客户端供下载
* @param $filePath
* 文件路径
* @param $saveAsFileName
* 下载的文件名
*/
function downloadFile($filePath,$saveAsFileName){
// 清空缓冲区并关闭输出缓冲
ob_end_clean();
//r: 以只读方式打开,b: 强制使用二进制模式
$fileHandle=fopen($filePath,"rb");
if($fileHandle===false){
echo "Can not find file: $filePath\n";
exit;
}
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Content-Length: '.filesize($filePath));
header("Content-Disposition: attachment; filename=\"$saveAsFileName\"");
while(!feof($fileHandle)) {
//从文件指针 handle 读取最多 length 个字节
echo fread($fileHandle, 32768);
}
fclose($fileHandle);
}

PHP代码片段的更多相关文章

  1. sublimetext3中保存代码片段

    在日常的开发工作中,不断重复上一次敲过的代码,有时确实感到伐木累."蓝瘦"(难受)."香菇"(想哭),大概表达的也是这样的心境吧!:grinning: 所以,在 ...

  2. Code Snippets 代码片段

    Code Snippets 代码片段       1.Title : 代码片段的标题 2.Summary : 代码片段的描述文字 3.Platform : 可以使用代码片段的平台,有IOS/OS X/ ...

  3. 10个 jQuery 代码片段,可以帮你快速开发。

    转载自:http://mp.weixin.qq.com/s/mMstI10vqwu8PvUwlLborw 1.返回顶部按钮 你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而 ...

  4. 代码片段添加智能提示,打造一款人见人爱的ORM框架

    SqlSugar ORM优点: 1.高性能,达到原生最高水准,比SqlHelper性能要高,比Dapper快30% 比EF快50% 2.支持多种数据库 ,sql版本更新最快,其它会定期更新,可以在多种 ...

  5. js/jquery/html前端开发常用到代码片段

    1.IE条件注释 条件注释简介 IE中的条件注释(Conditional comments)对IE的版本和IE非IE有优秀的区分能力,是WEB设计中常用的hack方法.条件注释只能用于IE5以上,IE ...

  6. Visual Studio 如何使用代码片段Code Snippet提高编程速度!!!

      使用Code Snippet简化Coding 在开发的项目的时候,你是否经常遇到需要重复编写一些类似的代码,比如是否经常会使用 for.foreach ? 在编写这两个循环语句的时候,你是一个字符 ...

  7. Visual Studio 的代码片段工具

    当安装完Visual Studio之后,会有附带一些原生的代码片段文件(*.snippet),对于vs2013参考目录如下: X:\Program Files (x86)\Microsoft Visu ...

  8. sublime代码片段

    创建方法:Tools > New Snippet 这时你会看到如下示例代码: <snippet>      <content><![CDATA[ Hello, ${ ...

  9. sublime 添加代码片段(snippets)

    1.工具-新代码片段(Tools -> New Snippet) 2. <snippet> <content><![CDATA[ if(\$rs && ...

  10. 在网站制作中随时可用的10个 HTML5 代码片段

    HTML 很容易写,但创建网页时,您经常需要重复做同样的任务,如创建表单.在这篇文章中,我收集了10个超有用的 HTML 代码片段,有 HTML5 启动模板.空白图片.打电话和发短信.自动完成等等,帮 ...

随机推荐

  1. 创建Jdbc封装工具类

    jdbc.propertie url=jdbc:mysql:///empye user=root password=root driver=com.mysql.jdbc.Driver 读取资源文件  ...

  2. xgboost 多gpu支持 编译

    xgboost 多gpu支持 编译 Ubuntu 18.04.2Linux 4.15.0-46-genericgcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0 cuda ...

  3. DCOS实践分享(6):基于DCOS的大数据应用分享

    Open DC/OS大中华区官方发布会在京隆重召开   DCOS领域诞生了一个100%开源的企业级Datacenter Operating System版本,即DC/OS.Linker Network ...

  4. jQuery ajax如何传多个值到后台页面,举例:

    一.js代码 <script type="text/JavaScript">$("#save_change_<{$aff.Id}>"). ...

  5. [Swift]LeetCode56. 合并区间 | Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  6. [Swift]LeetCode110. 平衡二叉树 | Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  7. [Swift]LeetCode243.最短单词距离 $ Shortest Word Distance

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  8. [Swift]LeetCode257. 二叉树的所有路径 | Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  9. [Swift]LeetCode706. 设计哈希映射 | Design HashMap

    Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...

  10. [Swift]LeetCode729. 我的日程安排表 I | My Calendar I

    Implement a MyCalendar class to store your events. A new event can be added if adding the event will ...