1.写日志基本方法
/**
* 写日志* @param unknown $path
* @param unknown $fileName
* @param unknown $data
* @param string $lock
* @return multitype:unknown
*/
function writeLog($path,$fileName,$data,$lock=false){
//global $resutl;
if(!empty($data)){
if(makeDir($path)){
$file = fopen($path.'/'.$fileName, 'a+');
//var_dump($file);
if($lock){
flock($file, LOCK_EX);
}
// var_dump($data);
// exit;
$resutl = array();
// foreach($data as $row){
if(fwrite($file, $data."\r\n")){
$resutl[] =$data;
}
// }
fclose($file);
}
}
return $resutl;
}
2.建立目录
/**
* 建立目录
* @param unknown $path
* @return string|boolean
*/
function makeDir($path) {
$path = str_replace(array('/', '\\', '//', '\\\\'), DIRECTORY_SEPARATOR, $path);
$dirs = explode(DIRECTORY_SEPARATOR, $path);
$tmp = '';
foreach ($dirs as $dir) {
$tmp .= $dir . DIRECTORY_SEPARATOR;
if (!file_exists($tmp) && !mkdir($tmp, 0777)) {
return $tmp;
}
}
return true;
}
3.字符串截取
/**
* 字符串截取
* @param string $str 原始字符串
* @param int $len 截取长度(中文/全角符号默认为 2个单位,英文/数字为 1。例如:长度 12 表示 6 个中文或全角字符或 12 个英文或数字)
* @param bool $dot (若字符串超过 $len 长度,则后面加 "...")
* @return string
*/
function my_truncate($str, $len, $dot = true) {
$i = 0;
$l = 0;
$c = 0;
$a = array();
while ($l <= $len) {
$t = substr($str, $i, 1);
if (ord($t) >= 224) {
$c = 3;
$t = substr($str, $i, $c);
$l += 2;
} elseif (ord($t) >= 192) {
$c = 2;
$t = substr($str, $i, $c);
$l += 2;
} else {
$c = 1;
$l++;
}
$i += $c;
if ($l > $len) break;
$a[] = $t;
}
$re = implode('', $a);
if (substr($str, $i, 1) !== false) {
array_pop($a);
($c == 1) and array_pop($a);
$re = implode('', $a);
$dot and $re .= '...';
}
return $re;
}
//根据用户的ip地址获取具体地址
function getIpName($ip){
global $address;
$url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=$ip&format=json";
//$json = file_get_contents($url);
$ctx = stream_context_create(array('http'=>array('timeout'=>10)));
$json = @file_get_contents($url, 0, $ctx);
$ip_info = json_decode($json,true);
return $ip_info;
}
//获取具体地址
function getIpAdr($ip){
$url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=$ip&format=json";
$address=file_get_contents($url);
$citys=json_decode($address,true);
$city=urldecode($citys['city']);
$county=urldecode($citys['country']);
$province=urldecode($citys['province']);
$cityAll=$county." ".$province." ".$city;
return $cityAll;
}