php通用函数--html--时间--文件大小--生成随机数
2013-01-14 11:21
260 查看
function format_html($str) {
return htmlentities($str, ENT_COMPAT, 'UTF-8');
}
function format_ago($time, $ago = false) {
$minute = 60;
$hour = $minute * 60;
$day = $hour * 24;
$when = $time;
if ($when >= 0)
$suffix = 'ago';
else {
$when = -$when;
$suffix = 'in the future';
}
if ($when > $day) {
$when = round($when / $day);
$what = 'day';
} else if ($when > $hour) {
$when = round($when / $hour);
$what = 'hour';
} else if ($when > $minute) {
$when = round($when / $minute);
$what = 'minute';
} else {
$what = 'second';
}
if ($when != 1) $what .= 's';
if ($ago) {
return "$when $what $suffix";
} else {
return "$when $what";
}
}
function format_size($size) {
$sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
if ($size == 0) {
return '0 B';
} else {
return round($size / pow(1024, ($i = floor(log($size, 1024)))), 1).' '.$sizes[$i];
}
}
function str_rand($length) {
$r = '';
for (; $length > 0; --$length) {
$r .= chr(rand(32, 126)); // 32 - 126 is the printable ascii range
}
return $r;
}
return htmlentities($str, ENT_COMPAT, 'UTF-8');
}
function format_ago($time, $ago = false) {
$minute = 60;
$hour = $minute * 60;
$day = $hour * 24;
$when = $time;
if ($when >= 0)
$suffix = 'ago';
else {
$when = -$when;
$suffix = 'in the future';
}
if ($when > $day) {
$when = round($when / $day);
$what = 'day';
} else if ($when > $hour) {
$when = round($when / $hour);
$what = 'hour';
} else if ($when > $minute) {
$when = round($when / $minute);
$what = 'minute';
} else {
$what = 'second';
}
if ($when != 1) $what .= 's';
if ($ago) {
return "$when $what $suffix";
} else {
return "$when $what";
}
}
function format_size($size) {
$sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
if ($size == 0) {
return '0 B';
} else {
return round($size / pow(1024, ($i = floor(log($size, 1024)))), 1).' '.$sizes[$i];
}
}
function str_rand($length) {
$r = '';
for (; $length > 0; --$length) {
$r .= chr(rand(32, 126)); // 32 - 126 is the printable ascii range
}
return $r;
}
相关文章推荐
- PHP模板编程及静态HTML文件生成学习
- php 批量生成html、txt文件
- php.ini 配置上传大文件大小和响应时间
- Log4j配置之按时间生成、按大小生成新文件
- php 批量生成html,txt文件的方法(实例代码)
- PHP代码为什么不能直接保存HTML文件——>PHP生成静态页面教程
- php文件管理,可以点击按照时间,大小,名称排序
- PHP——生成随机数和日期时间
- 根据时间随机数生成随机文件名 ,SpringMVC 上传文件
- logback.xml按时间和文件大小生成日志(分级别)
- PHP中生成静态html文件
- log4net按时间日期,文件大小和个数生成日志文件
- PHP生成HTML静态文件
- [轉載]史上最强php生成pdf文件,html转pdf文件方法
- php获得文件大小和文件创建时间的方法
- 史上最强php生成pdf文件,html转pdf文件方法
- php生成html静态文件
- PHP上传文件大小和时间限制
- 以php为例直接用html生成excel文件
- PHP读写文件的方法(生成HTML)