Skip to content

Instantly share code, notes, and snippets.

@mingyun
Created March 4, 2014 02:19
Show Gist options
  • Save mingyun/9339082 to your computer and use it in GitHub Desktop.
Save mingyun/9339082 to your computer and use it in GitHub Desktop.
去除utf8的bom
function checkBOM ($filename) {
global $auto;
$contents = file_get_contents($filename);
$charset[1] = substr($contents, 0, 1);
$charset[2] = substr($contents, 1, 1);
$charset[3] = substr($contents, 2, 1);
if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
if ($auto == 1) {
$rest = substr($contents, 3);
rewrite ($filename, $rest);
return ("<font color=red>BOM found, automatically removed.</font>");
}else {
return ("<font color=red>BOM found.</font>");
}
}else return ("BOM Not Found.");
}
echo transfer_time(1393487787);
//$time必须为Unix时间戳,如果不是请先用strtotime()将其转换成Unix时间戳。
function transfer_time($time)
{
$rtime = date("m-d H:i",$time);
$htime = date("H:i",$time);
$time = time() - $time;
if ($time < 60)
{
$str = '刚刚';
}
elseif ($time < 60 * 60)
{
$min = floor($time/60);
$str = $min.'分钟前';
}
elseif ($time < 60 * 60 * 24)
{
$h = floor($time/(60*60));
$str = $h.'小时前 '.$htime;
}
elseif ($time < 60 * 60 * 24 * 3)
{
$d = floor($time/(60*60*24));
if($d==1)
$str = '昨天 '.$rtime;
else
$str = '前天 '.$rtime;
}
else
{
$str = $rtime;
}
return $str;
}
function mbstrlen($str)
{
echo $len = strlen($str);//17
if ($len <= 0)
{
return 0;
}
$count = 0;
for ($i = 0; $i < $len; $i++)
{
$count++;
if (ord($str{$i}) >= 0x80)
{
$i += 2;//利用单字节字符的ASCII码小于0x80。至于要跳过几个字节
}
}
return $count;
}
echo "output: " . mbstrlen("中国so强大!") . "\n";//7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment