php常用封装函数总结
####用于处理android 和 iOS的图片上传
/**
* 用于处理android 和 iOS的图片上传]
* @param [type] $byte [图片]
* @param [type] $name [图片名]
* @param [type] $lx [类型]
*/
function _Android_or_iOS_img_up($byte, $name, $lx)
{
if (empty($byte)) {
$data['zt'] = 'no';
return $data;
}
$path = "public/uploads/channel/" . $name . ".png"; // 产生随机唯一的名字作为文件名
$res = false;
$res = @file_put_contents($path, base64_decode($byte));
if (!$res) {
header("Content-Type: application/octet-stream");
$byte = str_replace(' ', '', $byte); //处理数据
$byte = str_ireplace("<", '', $byte);
$byte = str_ireplace(">", '', $byte);
$byte = pack("H*", $byte); //16进制转换成二进制
$res = @file_put_contents($path, $byte);
}
return $path;
}
判断访问的手机类型
function is_phone_type(){
if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
return 'Ios';
}else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
return 'Android';
}else{
return 'other';
}
}
/***
* @param $txt
* @param string $key
*http post请求
* $arr 数组
*/
function httpPost($url,$arr){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
/**
* 验证输入的邮件地址是否合法
*
* @param string $email 需要验证的邮件地址
*
* @return bool
*/
function is_email($user_email)
{
$chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,5}\$/i";
if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false)
{
if (preg_match($chars, $user_email))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
function strFilter($str){
$str = str_replace('`', '', $str);
$str = str_replace('·', '', $str);
$str = str_replace('~', '', $str);
$str = str_replace('!', '', $str);
$str = str_replace('!', '', $str);
$str = str_replace('@', '', $str);
$str = str_replace('#', '', $str);
$str = str_replace('$', '', $str);
$str = str_replace('¥', '', $str);
$str = str_replace('%', '', $str);
$str = str_replace('^', '', $str);
$str = str_replace('……', '', $str);
$str = str_replace('&', '', $str);
$str = str_replace('*', '', $str);
$str = str_replace('(', '', $str);
$str = str_replace(')', '', $str);
$str = str_replace('(', '', $str);
$str = str_replace(')', '', $str);
$str = str_replace('-', '', $str);
$str = str_replace('_', '', $str);
$str = str_replace('——', '', $str);
$str = str_replace('+', '', $str);
$str = str_replace('=', '', $str);
$str = str_replace('|', '', $str);
$str = str_replace('\\', '', $str);
$str = str_replace('[', '', $str);
$str = str_replace(']', '', $str);
$str = str_replace('【', '', $str);
$str = str_replace('】', '', $str);
$str = str_replace('{', '', $str);
$str = str_replace('}', '', $str);
$str = str_replace(';', '', $str);
$str = str_replace(';', '', $str);
$str = str_replace(':', '', $str);
$str = str_replace(':', '', $str);
$str = str_replace('\'', '', $str);
$str = str_replace('"', '', $str);
$str = str_replace('“', '', $str);
$str = str_replace('”', '', $str);
$str = str_replace(',', '', $str);
$str = str_replace(',', '', $str);
$str = str_replace('<', '', $str);
$str = str_replace('>', '', $str);
$str = str_replace('《', '', $str);
$str = str_replace('》', '', $str);
$str = str_replace('.', '', $str);
$str = str_replace('。', '', $str);
$str = str_replace('/', '', $str);
$str = str_replace('、', '', $str);
$str = str_replace('?', '', $str);
$str = str_replace('?', '', $str);
return trim($str);
}
//频道是否是微信浏览器访问
function is_weixin(){
if (strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessenger') !== false ){
return true;
}
return false;
}
function _https_get($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Errno'.curl_error($ch);
}
curl_close($ch);
$arr= json_decode($tmpInfo,true);
return $arr;
}
function Sec2Time($time){
if(is_numeric($time)){
$value = array(
"years" => 0, "days" => 0, "hours" => 0,
"minutes" => 0, "seconds" => 0,
);
if($time >= 3600){
$value["hours"] = floor($time/3600);
$time = ($time%3600);
}
if($time >= 60){
$value["minutes"] = floor($time/60);
$time = ($time%60);
}
$value["seconds"] = floor($time);
if($value["hours"]>=0 && $value["hours"]<10){
$hours = '0'.$value["hours"];
}else{
$hours = $value["hours"];
}
if($value["minutes"]>=0 && $value["minutes"]<10){
$minutes = '0'.$value["minutes"];
}else{
$minutes = $value["minutes"];
}
if($value["seconds"]>=0 && $value["seconds"]<10){
$seconds = '0'.$value["seconds"];
}else{
$seconds = $value["seconds"];
}
$t= $hours .":". $minutes .":".$seconds;
return $t;
}else{
return "";
}
}
/**
* 数组 转 对象
*
* @param array $arr 数组
* @return object
*/
public function array_to_object($arr) {
if (gettype($arr) != 'array') {
return;
}
foreach ($arr as $k => $v) {
if (gettype($v) == 'array' || getType($v) == 'object') {
$arr[$k] = (object)$this->array_to_object($v);
}
}
return (object)$arr;
}
/**
* 对象 转 数组
*
* @param object $obj 对象
* @return array
*/
public function object_to_array($obj) {
$obj = (array)$obj;
foreach ($obj as $k => $v) {
if (gettype($v) == 'resource') {
return;
}
if (gettype($v) == 'object' || gettype($v) == 'array') {
$obj[$k] = (array)$this->object_to_array($v);
}
}
return $obj;
}