PHP Common function Encapsulation

Source: Internet
Author: User
This article is to share the content of PHP commonly used function encapsulation, interested friends can have a look

1. Determine if 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 ';

2. Text Editor Intercept:


/** * Capture text in Rich text as part of * @param $content * @return string */function html_substr_content ($content, $length =100) {    $ Content = Htmlspecialchars_decode ($content);      Convert some of the predefined HTML entities to characters      $content = Str_replace ("", "", $content);     Replace the space with an empty      $content = Strip_tags ($content);                 The function strips HTML, XML, and PHP tags from the string to get plain text content      $con = mb_substr ($content, 0, $length, "Utf-8");   Returns the character of the first 100 string length in the string return      $con;}

3. Delete an empty array in the array


Delete empty array in data (search in common) function Where_data ($data) {    foreach ($data as $k + $v) {        if (empty ($v) && $v! = ' 0 ' ) {            unset ($data [$k]);        }    }    return $data;}

4. Converting an array into an object


/** * Array to Object * @param object $obj objects * @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;}

5.xml Conversion Array


The XML converts an array of functions function Xmltoarray ($simpleXmlElement) {    $simpleXmlElement = (array) $simpleXmlElement;    foreach ($simpleXmlElement as $k = + $v) {        if ($v instanceof simplexmlelement | | is_array ($v)) {            $simpleXmlEle ment[$k] = Xmltoarray ($v);        }    }    return $simpleXmlElement;}

6. Generate a unique order number


function Generate_out_trade_no () {    $a = date (' Ymd '). substr (Implode (NULL, Array_map (' Ord ', Str_split (substr ( Uniqid (), 7, 1)), 0, 8);    Return ' F_ '. $a. Rand (10000000, 99999999);}

7. Determine whether it is mobile or PC-side


* * To determine if the current user is the PC or the phone end returns true for the phone side false for the PC side * @return Boolean *//** * Whether the Mobile Access access * * @return bool */function    IsMobile () {///If there is http_x_wap_profile, it must be the mobile device if (Isset ($_server[' http_x_wap_profile ')) return true; If the VIA information contains WAP, it must be a mobile device, some service associations block the information if (Isset ($_server[' Http_via ')) {//Cannot find for flase, otherwise true return St Ristr ($_server[' Http_via '), "WAP")?    True:false; }//Brain residue method, determine the mobile phone sent by the client flag, compatibility needs to be improved 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 '; Look for the keyword if (Preg_match ("/(") of the phone's browser from Http_user_agent. Implode (' | ', $clientkeywords).    ")/I", Strtolower ($_server[' http_user_agent '))) return true; }    //protocol method, because there may be inaccuracies, put to the Last Judgment if (Isset ($_server[' http_accept ')) {//If only WML is supported and HTML is not supported that must be mobile device//If WML and H are supported TML but WML before HTML is the mobile device if ((Strpos ($_server[' http_accept '), ' VND.WAP.WML ')!== false) && (Strpos ($_server[' H Ttp_accept '], ' text/html ') = = = False | |        (Strpos ($_server[' http_accept '), ' vnd.wap.wml ') < Strpos ($_server[' http_accept '], ' text/html '))))        {return true; }} return false;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.