Home > Backend Development > PHP Tutorial > PHP common function encapsulation

PHP common function encapsulation

不言
Release: 2023-03-23 07:42:02
Original
3536 people have browsed it

The content shared with you in this article is the encapsulation of common functions in PHP. Friends who are interested can take a look

1. Determine whether the request is http or https


$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
Copy after login

2. Text editor interception:


/** * 将富文本中文字截取其中的一部分 * @param $content * @return string */function html_substr_content($content,$length=100)
{    $content = htmlspecialchars_decode($content);      //把一些预定义的 HTML 实体转换为字符  
    $content = str_replace(" ", "", $content);     //将空格替换成空  
    $content = strip_tags($content);                 //函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容  
    $con = mb_substr($content, 0, $length, "utf-8");   //返回字符串中的前100字符串长度的字符  
    return $con;}
Copy after login

3. Delete the empty array in the array


//删除数据中的空数组(搜索中常用)function where_data($data)
{    foreach ($data as $k => $v) {        if (empty($v) && $v !='0') {            unset($data[$k]);        }
    }    return $data;}
Copy after login

4.Convert array to object


/** * 数组 转 对象 * @param object $obj 对象 * @return array */function array2object_l($array) {    if (is_array($array)) {        $obj = new StdClass();        foreach ($array as $key => $val){            $obj->$key = $val;        }
    } else {        $obj = $array;    }    return $obj;}
Copy after login

5.Convert xml to array


//xml 转换成数组函数function xmlToArray($simpleXmlElement)
{    $simpleXmlElement = (array)$simpleXmlElement;    foreach ($simpleXmlElement as $k => $v) {        if ($v instanceof SimpleXMLElement || is_array($v)) {            $simpleXmlElement[$k] = xmlToArray($v);        }
    }    return $simpleXmlElement;}
Copy after login

6.Generate Unique order number


function generate_out_trade_no()
{    $a = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);    return 'F_' . $a . rand(10000000, 99999999);}
Copy after login

7. Determine whether it is a mobile or PC terminal


** * 判断当前访问的用户是  PC端  还是 手机端  返回true 为手机端  false 为PC 端 * @return boolean *//**  * 是否移动端访问访问  *  * @return bool  */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 (isset ($_SERVER['HTTP_USER_AGENT']))
    {        $clientkeywords = array ('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile');        // 从HTTP_USER_AGENT中查找手机浏览器的关键字        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))            return true;    }    // 协议法,因为有可能不准确,放到最后判断    if (isset ($_SERVER['HTTP_ACCEPT']))
    {        // 如果只支持wml并且不支持html那一定是移动设备        // 如果支持wml和html但是wml在html之前则是移动设备        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
        {            return true;        }
    }    return false;}
Copy after login

Related recommendations:

php encapsulated search class example

php encapsulated mongodb operation class code sharing

The above is the detailed content of PHP common function encapsulation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template