php 自己封装的一些函数

手机归属地函数

function get_mobile_area($phone){
  $sms = array('province'=>'', 'supplier'=>''); //初始化变量
  //根据淘宝的数据库调用返回值
  $url = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=".$phone."&t=".time();
  $content = file_get_contents($url);
  $sms['province'] = substr($content, "56", "4"); //截取字符串
  $sms['supplier'] = substr($content, "81", "4");
  $att= $sms['province'].$sms['supplier'];
  return $att;

}

//转化成utf-8格式
function charsetToUTF8($content)
{
if (is_array($content)) {
foreach ($content as $k => $v) {
if (is_array($v)) {
$content[$k] = charsetToUTF8($v);
} else {
$encode = mb_detect_encoding($v, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
if ($encode == 'EUC-CN') {
$content[$k] = iconv('GBK', 'UTF-8', $v);
}
}
}
} else {
$encode = mb_detect_encoding($content, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
if ($encode == 'EUC-CN') {
$content = iconv('GBK', 'UTF-8', $content);
}
}
return $content;
}

//将数组转成对象

function array2object($array) {
if (is_array($array)) {
$obj = new StdClass();
foreach ($array as $key => $val){
$obj->$key = $val;
}
}
else { $obj = $array; }
return $obj;
}

//将对象转化成数组

function object2array($object) {
if (is_object($object)) {
foreach ($object as $key => $value) {
$array[$key] = $value;
}
}
else {
$array = $object;
}
return $array;
}

/**
* 加载函数库
*/
function load_function($function){
$filename = FUNCTION_DIR.$function.'.function.php';
if(file_exists($filename)){
require($filename);
}else {
pr($filename.'is not exist',1);
}
}

//字符串加转意
function add_slashes($string){
if(!GLOBALS['magic_quotes_gpc']){
if(is_array($string)){
foreach($string){
$string[$key] = add_slashes($val);
}
}else {
$string = addslashes($string);
}
}
return $string;
}

/**
*二维数组按照制定的数组进行排序
*/

function array_sort($arr,$keys,$type = 'asc'){
$keysvalue = $new_array = array();
foreach($arr as $k =>$v){
$keysvalue[$k]= $v[$keys];
}
if($type == 'asc'){
asort($keysvalue);
}else{
arsort($keysvalue);
}
reset($keysvalue);
foreach($keysvalue as $k=>$v){
$new_array[$k] = $arr[$k];
}
return $new_array;
}

/**
*得到服务器短的信息
*/

function getServerInfo($link = null){
return mysql_get_server_info($link);
}


//得到主机的信息

function getHostInfo($link = mull){
return mysql_get_host_info(info);
}

//得到协议信息
function getProtoInfo($link = null){
return mysql_get_proto_info($link);
}

//数组和字符串的互相转化

$array=explode(separator,$string);

$string=implode(glue,$array);

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

//封装函数干掉字符串两端的空格
public function trimall($str)
{
$qian=array(" "," "," "," "," ");
$hou=array("","","","","");
return str_replace($qian,$hou,$str);
}

//封装搜索函数
public function doSousuo($k,$table,$file)
{
$total = db($table)->where($file,'like',$k)->select();
$t = count($total);
$this->assign('t',$t);
return $total;
}

//用户搜索功能

public function yhcx()
{

$key = $this->trimall(input('post.'));
$keys = $key['keys'];
if(!$keys){
$this->error('关键字不能为空','/admin/index/xmcx');
die;
}
$k = "%$keys%";
$table = 'user';
$file = 'name|realName';
$total =$this->doSousuo($k,$table,$file);
if(!$total){
$this->error('数据库中没有相关数据,你可查询百度。','/admin/index/xmcx');
die;
}
$this->assign('total',$total);
return $this->fetch();


}

  

.╭⌒╮ ─ ● ─ ╭ ⌒╮╭⌒╮. / │ \ .╭⌒╮ .╭ ⌒╮. ╭ ⌒╮. ╭ ⌒╮. ╭ ⌒╮ ╭⌒╮ ╭⌒╮ =====●=============== 希望你别迷路了, 希望你交到好朋友, 希望你别再被人欺负, 希望你幸福, 希望你一个人, 也能够坚强。
原文地址:https://www.cnblogs.com/weiwozui/p/7183471.html