常用函数

  • php强制下载文件

    //有时我们不想让浏览器直接打开文件,如PDF文件,而是要直接下载文件,那么以下函数可以强制下载文件,函数中使用了application/octet-stream头类型 function download($filename) { if ((isset($filename))&&(file_exists($filename))){ header("Content-length: ".filesize($filename)); header('Content-Type: application/octet-stream'); header('Content-Disposition

  • php获取当前页面的完整url

    PHP获取当前页面的完整url,如:"http://www.pengxb.com/article/23"//以下函数可以获取当前页面的URL,不管是http还是https function curPageURL() { $pageURL = 'http'; if (!empty($_SERVER['HTTPS'])) {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"]

  • curl请求函数

    1.封装请求函数if(!function_exists('curl_request')){ //发送curl请求 function curl_request($url, $type = false, $params = [], $https=false) { //调用curl_init() 初始化请求 $ch = curl_init($url); //调用curl_setopt()设置请求选项 if($type){ //true 发送post请求 false 默认发送get请求 //post请求 设置请求方式 curl_setopt($ch, CURLOPT_POST, true); //设置

  • 是否移动端访问访问

    function isMobile(){ // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) return true; // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息 if (isset ($_SERVER['HTTP_VIA'])) { // 找不到为flase,否则为true return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false; } // 脑残法,判断手机发送的客户端标志,兼容性有待提高 if (is

  • PHP验证手机号码正则表达式

    function is_mobile_phone($mobile_phone){ $chars = "/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$/"; if (preg_match($chars, $mobile_phone)) { return true; } return false;} // 验证手机号码public function checkMobile($mobile){ // 验证中国大陆手机号码格式 $isMobileNumber = preg_match('

  • 验证输入的邮件地址是否合法

    function is_email($user_email){ $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i"; if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false) { if (preg_match($chars, $user_email)) { return true; } else { return false; } } else { return false; }}

  • 无限极分类函数

    1.递归函数: 实现无限级分类列表# 形式1:实现无限级分类列表if(!function_exists('get_cate_list')){ //递归函数 实现无限级分类列表 function get_cate_list($list,$pid=0,$level=0) { static $tree = array(); foreach($list as $row) { if($row['pid']==$pid) { $row['level'] = $level; $tree[] = $row; get_cate_list($list, $row['id'], $level + 1); } } r