<?php
function characet($data,$code='UTF-8'){
if( !empty($data) ){
$fileType = mb_detect_encoding($data , array('UTF-8','GBK','LATIN1','BIG5')) ;
if( $fileType != $code){
$data = mb_convert_encoding($data ,$code, $fileType);
}
}
return $data;
}
?>
function getKeyValues($arr, $key)
{
$ret = [];
foreach ($arr as $row) {
if (is_array($row) && array_key_exists($key, $row)) {
$ret[] = $row[$key];
} elseif ($row instanceof \ArrayObject) {
$row->offsetExists($key) && $ret[] = $row->offsetGet($key);
}
}
return $ret;
}
/**
* 检测是否是手机访问
*/
function is_mobile()
{
$useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$useragent_commentsblock = preg_match('|\(.*?\)|', $useragent, $matches) > 0 ? $matches[0] : '';
function _is_mobile($substrs, $text)
{
foreach ($substrs as $substr)
if (false !== strpos($text, $substr)) {
return true;
}
return false;
}
$mobile_os_list = array('Google Wireless Transcoder', 'Windows CE', 'WindowsCE', 'Symbian', 'Android', 'armv6l', 'armv5', 'Mobile', 'CentOS', 'mowser', 'AvantGo', 'Opera Mobi', 'J2ME/MIDP', 'Smartphone', 'Go.Web', 'Palm', 'iPAQ');
$mobile_token_list = array('Profile/MIDP', 'Configuration/CLDC-', '160×160', '176×220', '240×240', '240×320', '320×240', 'UP.Browser', 'UP.Link', 'SymbianOS', 'PalmOS', 'PocketPC', 'SonyEricsson', 'Nokia', 'BlackBerry', 'Vodafone', 'BenQ', 'Novarra-Vision', 'Iris', 'NetFront', 'HTC_', 'Xda_', 'SAMSUNG-SGH', 'Wapaka', 'DoCoMo', 'iPhone', 'iPod');
$found_mobile = _is_mobile($mobile_os_list, $useragent_commentsblock) ||
_is_mobile($mobile_token_list, $useragent);
if ($found_mobile) {
return true;
} else {
return false;
}
}
//剔除a标签
function tichuA($str){
$str1 = preg_replace("/<a[^>]*>/","", $str);
$str2 = preg_replace("/<\/a>/","", $str1);
return $str2;
}
直接用 floatval($arg) 来转换,既可以保留有效的小数点,也可以去掉后面多余的0, 其中arg 为变量
$number1 = "98.00";
$number2 = "98.76";
echo floatval($number1); //输出结果:98
echo floatval($number2); //输出结果:98.76
/**
*
* curl 支持post
* @param string $base_url 基础链接
* @param array $query_data 需要请求的数据
* @param string $method 方法 get/post
* @param boolean $ssl 关闭ssl验证
* @param integer $exe_timeout 执行超时时间
* @param integer $conn_timeout 连接超时时间
* @param integer $dns_timeout dns超时时间
*/
function tx_curl($base_url, $query_data, $method = 'get', $ssl = true, $exe_timeout = 10, $conn_timeout = 10, $dns_timeout = 3600)
{
$ch = curl_init();
if ( $method == 'get' ) {
//method get
if ( ( !empty($query_data) )
&& ( is_array($query_data) )
){
$connect_symbol = (strpos($base_url, '?')) ? '&' : '?';
foreach($query_data as $key => $val) {
if ( is_array($val) ) {
$val = serialize($val);
}
$base_url .= $connect_symbol . $key . '=' . rawurlencode($val);
$connect_symbol = '&';
}
}
} else {
if ( ( !empty($query_data) )
&& ( is_array($query_data) )
){
foreach($query_data as $key => $val) {
if ( is_array($val) ) {
$query_data[$key] = serialize($val);
}
}
}
//method post
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_data);
}
curl_setopt($ch, CURLOPT_URL, $base_url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $conn_timeout);
curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, $dns_timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $exe_timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// 关闭ssl验证
if($ssl){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
$output = curl_exec($ch);
if ( $output === FALSE )
$output = '';
curl_close($ch);
return $output;
}
/***********高德地图 根据地址获取经纬度************/
function getCoorByAddr($address)
{
$key = C('GAODE_KEY');
// $key = 'd20f62e5a58433a20e75c015a4f054fe';
$url = "http://restapi.amap.com/v3/geocode/geo?key={$key}&address={$address}";
$arr = file_get_contents($url);
$newarr = json_decode($arr, true);
if($newarr['info']=='OK'){
$coor = $newarr['geocodes'][0]['location'];
$coor =explode(',',$coor);
// var_dump($coor);
return array(
'coor_x'=>$coor[0],
'coor_y'=>$coor[1],
);
}
}
public function importFromExcel($excel_url)
{
//Loader::import('phpexcel.PHPExcel');
Loader::import('phpexcel.PHPExcel.IOFactory');
// $phpExcel = new \PHPExcel();
$fileExt = getExt($excel_url);
if ($fileExt == 'xls') {
$reader = \PHPExcel_IOFactory::createReader('Excel5');
} else {
$reader = \PHPExcel_IOFactory::createReader('Excel2007');
}
//$reader = \PHPExcel_IOFactory::createReader('Excel2007');
//$reader = \PHPExcel_IOFactory::createReader('Excel5');
$allSheets = $reader->load('/www/FenFa/public'.$excel_url);
$excel_arr = $allSheets->getSheet(0)->toArray();
array_shift($excel_arr);
$data = [];
foreach ($excel_arr as $k=>$v) {
if ($v[1]) {
$data[$k]['data_time'] = strtotime(str_replace('.', '-', $v[0]));
$data[$k]['cooperation_id'] = $v[1];
$data[$k]['settlement_price'] = $v[2];
$data[$k]['settlement_ratio'] = $v[3];
$data[$k]['settlement_num'] = $v[4];
}
}
foreach ($data as $v) {
if ($v['cooperation_id']) {
Db::name('data')->insert($v);
}
}
}