1. file_get_contents无效时使用
其返回false,使用curl调用
function file_get_content($url) { if (function_exists('file_get_contents')) { $file_contents = @file_get_contents($url); } if ($file_contents =='') { $ch = curl_init(); $timeout = 30; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); } return $file_contents; }
2. PHP生成一个随机字符串
如果不需要可阅读的字符串,使用此函数替代,即可创建一个随机字符串,作为用户的随机密码等。
function generate_rand($l){ $c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; srand((double)microtime()*1000000); for($i=0; $i<$l; $i++) { $rand.= $c[rand()%strlen($c)]; } return $rand; }
3. 脚本运行时间记录
当需要检测脚本运行时间时使用:监测时间开始点:$start_time = runTime();监测时间结束点:$run_time = runTime($start_time);
function runTime($start_time = 0){ if($start_time){ return runTime()-$start_time; }else{ list($usec, $sec) = explode(" ", microtime()); $a = ((float)$usec + (float)$sec)*1000; return $a; } }
4. xml->array
接口开发的过程中难免用到xml格式,把它转换成array
function xmlToArray($xml){ libxml_disable_entity_loader(true); $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $val = json_decode(json_encode($xmlstring),true); return $val; }
5. array->xml
function arrayToXml($arr){ $xml = "<root>"; foreach ($arr as $key=>$val){ if(is_array($val)){ $xml.="<".$key.">".arrayToXml($val)."</".$key.">"; }else{ $xml.="<".$key.">".$val."</".$key.">"; } } $xml.="</root>"; return $xml ;}
6. 写文件缓存
当访问量上来时没有内存缓存redis,memcache时,可以先用文件缓存挡一下。
tips:可以json_encode + md5 作为文件的名称,方便缓存不同查询条件下的内容。
function w_cache($data,$file_name,$dir,$append = false){ $ret = array('time'=>time(),'data'=>$data); $ret = json_encode($ret); if (!@mkdir('/var/logdata/'.$dir.'/', 0755)){ if($append){ file_put_contents('/var/logdata/'.$dir.'/'.$file_name.date('Y-m-d'),$ret,FILE_APPEND); }else{ file_put_contents('/var/logdata/'.$dir.'/'.$file_name,$ret); } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
7. 读文件缓存
function r_cache($min ,$file_name,$dir){ $cache_time = $min * 60 ; $file_name = '/var/logdata/121shop/'.$dir.'/'.$file_name; if(!is_file($file_name)){ return false; } $data = file_get_contents($file_name); if(empty($data)){ return false; } $data = json_decode($data,true); $now = time(); $data_time = $data['time']; if($now-$data_time>$cache_time){ return false; } return empty($data)?false:$data['data']; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
8. 配置文件读取 C方法
$config_db = include_once PATH.”/Conf/db.php”; //引入数据库配置文件,返回数组
function C($name=null, $value=null) { static $_config = array(); if (empty($name)) return $_config; if (is_string($name)) { if (!strpos($name, '.')) { $name = strtolower($name); if (is_null($value)) return isset($_config[$name]) ? $_config[$name] : null; $_config[$name] = $value; return; } $name = explode('.', $name); $name[0] = strtolower($name[0]); if (is_null($value)) return isset($_config[$name[0]][$name[1]]) ? $_config[$name[0]][$name[1]] : null; $_config[$name[0]][$name[1]] = $value; return; } if (is_array($name)){ return $_config = array_merge($_config, array_change_key_case($name)); } return null; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
9. PHP获取IP 地址
该函数将获取用户的真实 IP 地址,即便他使用代理服务器。
function getRealIpAddr() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip=$_SERVER['HTTP_CLIENT_IP']; }elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip=$_SERVER['REMOTE_ADDR']; } return $ip; }
取上一步来源地址
/** * 取上一步来源地址 * * @param * @return string 字符串类型的返回结果 */function getReferer(){ return empty($_SERVER['HTTP_REFERER'])?'':$_SERVER['HTTP_REFERER'];}
10. PHP寻找两个字符串的相似性
PHP 提供了一个极少使用的 similar_text 函数,但此函数非常有用,用于比较两个字符串并返回相似程度的百分比。
similar_text($string1, $string2, $percent);
11. unicode转为utf8
/** * unicode转为utf8 * @param string $str 待转的字符串 * @return string */function unicodeToUtf8($str, $order = "little"){ $utf8string =""; $n=strlen($str); for ($i=0;$i<$n ;$i++ ) { if ($order=="little") { $val = str_pad(dechex(ord($str[$i+1])), 2, 0, STR_PAD_LEFT) . str_pad(dechex(ord($str[$i])), 2, 0, STR_PAD_LEFT); } else { $val = str_pad(dechex(ord($str[$i])), 2, 0, STR_PAD_LEFT) . str_pad(dechex(ord($str[$i+1])), 2, 0, STR_PAD_LEFT); } $val = intval($val,16); // 由于上次的.连接,导致$val变为字符串,这里得转回来。 $i++; // 两个字节表示一个unicode字符。 $c = ""; if($val < 0x7F) { // 0000-007F $c .= chr($val); } elseif($val < 0x800) { // 0080-07F0 $c .= chr(0xC0 | ($val / 64)); $c .= chr(0x80 | ($val % 64)); } else { // 0800-FFFF $c .= chr(0xE0 | (($val / 64) / 64)); $c .= chr(0x80 | (($val / 64) % 64)); $c .= chr(0x80 | ($val % 64)); } $utf8string .= $c; } /* 去除bom标记 才能使内置的iconv函数正确转换 */ if (ord(substr($utf8string,0,1)) == 0xEF && ord(substr($utf8string,1,2)) == 0xBB && ord(substr($utf8string,2,1)) == 0xBF) { $utf8string = substr($utf8string,3); } return $utf8string;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
12. 价格格式化
/*** 价格格式化** @param int $price* @return string $price_format*/function PriceFormat($price) { $price_format= number_format($price,2,'.',''); return $price_format;}
13. j s判断访问网站的类型,ios or android
var u = navigator.userAgent; var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); alert(isAndroid);alert(isiOS);