php自己準備関数
8040 ワード
/**
//http
function https_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
//
function format_date($time){
$time = strtotime($time);
$t=time()-$time;
$f=array(
'31536000'=>' ',
'2592000'=>' ',
'604800'=>' ',
'86400'=>' ',
'3600'=>' ',
'60'=>' ',
'1'=>' '
);
foreach ($f as $k=>$v) {
if (0 !=$c=floor($t/(int)$k)) {
return $c.$v.' ';
}
}
}
/**
/**
* ,
*@param lng1,lng2
*@param lat1,lat2
*@return float ,
*@author www.Alixixi.com
**/
//function getdistance($lng1,$lat1,$lng2,$lat2){
function getdistance($in_location,$coordination){
$in_location = explode(',',$in_location);
list($lng1, $lat1) = $in_location;
$coordination = explode(',',$coordination);
list($lng2, $lat2) = $coordination;
//
$radLat1=deg2rad($lat1);//deg2rad()
$radLat2=deg2rad($lat2);
$radLng1=deg2rad($lng1);
$radLng2=deg2rad($lng2);
$a=$radLat1-$radLat2;
$b=$radLng1-$radLng2;
$s=2*asin(sqrt(pow(sin($a/2),2)+cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)))*6378.137*1000;
//$s = convertMeter($s);
return $s;
}
/**
* html img
@param string $content @return array */function sp_getcontent_imgs($content){ import("phpQuery");\phpQuery::newDocumentHTML($content); $pq=pq(); $imgs=$pq->find("img"); $imgs_data=array(); if($imgs->length()){ foreach ($imgs as $img){ $img=pq($img); $im['src']=$img->attr("src"); $im['title']=$img->attr("title"); $im['alt']=$img->attr("alt"); $imgs_data[]=$im; } }\phpQuery::$documents=null; return $imgs_data; }
/**
をphpファイル/function sp_に save_var($path,$value){ $ret = file_put_contents($path, ""); return $ret; }
/
* ランダム @param int$len @return string */function sp_random_string($len = 6,$type=1) { if($type){ $chars = array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7","8","9");$charsLen=count($chars)-1;shuffle($chars);// を す$output=";for($i=0;$i<$len;$i+){$output.=$chars[mt_rand(0,$charsLen)];}else{ return mt_rand(1000, 9999); } return $output; }
/**
に1つの が するかどうかを する .@param[string]$value[ する ] @param[array]$array[ する をチェック] @return[bool][true/false]*/function deep_in_array($value,$array){foreach($array as$item){if(!is_array($item){// if($item=$value){return true;}else { continue; } } if(in_array($value, $item)) { //
return true;
} else if(deep_in_array($value, $item)) { //
return true;
}
} return false; }
/**
チーム を @param[string]$name @param[int]$rank レベル @param[bool]$activationがアクティブな を するかどうか@return[type][description]*/function geteTeam($name,$rank=26,$activation=false,$lev=0){global$num;$num++;if(!$name) returnfalse;if($activation){//アクティブな $userList=M('member')->field('id,account,active')->where(array('ference'=>$name,'''''','''''''('ference'=>$name,',''''''',''''''''''''''''Active'=>1)->select();}else{ $userList = M('member')->field('id,account,active')->where(array('references'=>$name))->select(); } if($userList && $num<=$rank){ foreach ($userList as $key => $value) { $userList[$key]['lev'] = $num; $userList['list'] = getTeam($value['account'],$rank,$activation); $arr[] = $userList; } } return $arr; }
function arr_to_xml($arr, $dom = 0, $item = 0) {
if (! $dom) {
$dom = new DOMDocument ("1.0","UTF-8");
} if (! $item) {
$ccc = array_keys ( $arr );
if ($ccc [0] == 'envelope') {
$str_head = 'request';
} else {
$str_head = 'envelope';
}
$item = $dom->createElement ( $str_head );
$dom->appendChild ( $item );
}
foreach ( $arr as $key => $val ) {
$itemx = $dom->createElement ( is_string ( $key ) ? $key : "record" );
$item->appendChild ( $itemx );
if (! is_array ( $val )) {
$text = $dom->createTextNode ( $val );
$itemx->appendChild ( $text );
} else {
arr_to_xml ( $val, $dom, $itemx );
}
}
return $dom->saveXML ();
}
function xml_to_array($xml) { $reg = "/]?>([\x00-\xFF]?)/"; if (preg_match_all ( $reg, $xml, $matches )) { $count = count ( $matches [0] ); $arr = array (); for($i = 0; $i < $count; $i++) { $key = $matches [1] [$i];
$val = xml_to_array ( $matches [2] [$i] ); //
if (array_key_exists ( $key, $arr )) {
if (is_array ( $arr [$key] )) {
if (! array_key_exists ( 0, $arr [$key] )) {
$arr [$key] = array (
$arr [$key]
);
}
} else {
$arr [$key] = array (
$arr [$key]
);
}
$arr [$key] [] = $val;
} else {
$arr [$key] = $val;
}
}
return $arr;
} else {
return $xml;
}
}