Some common PHP functions (sorting)

Source: Internet
Author: User
Some common PHP functions (sorting)
/*** Response interval * zouhao619@gmail.com * // *** get the current millisecond * @ return string */function get_millisecond () {$ t = explode ("", microtime (); $ t = $ t [1]. ($ t [0] * 1000); $ t2 = explode (". ", $ t); return $ t2 [0];} /*** curl simulates http/https post requests * @ param string $ url request url * @ param array $ data request parameter * @ return string url content */function curl_post ($ url, $ data = array () {$ ch = curl_init ($ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSF ER, true); // curl_setopt ($ ch, CURLOPT_POST, true) is returned for obtaining data; // When enabled, a conventional POST request is sent. the type is: application/x-www-form-urlencoded, just like submitting a form. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // The "POST" operation in HTTP. If you want to transfer a file, you need a file name starting with @ if (substr ($ url,) = 'https') {curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );} $ content = curl_exec ($ ch); curl_close ($ ch); return $ content ;} /*** curl simulate http get request * @ param string $ url request url * @ param string | array $ data request parameter * @ return string url content */function curl_get ($ url, $ data = array () {$ url = rtrim ($ url, '/'); if (! Empty ($ data) {if (is_array ($ data) {$ first = true; foreach ($ data as $ k = >$ v) {if ($ first) {$ url. = '? '; $ First = false;} else {$ url. = '&';} $ url. = "{$ k }={$ v}" ;}} else {$ data = ltrim ('? ', $ Data); $ url. = '? '. $ Data ;}$ ch = curl_init ($ url); curl_setopt ($ ch, CURLOPT_HEADER, false); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); // obtain the data and return if (substr ($ url,) = 'https') {curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );} $ content = curl_exec ($ ch); curl_close ($ ch); return $ content ;} /*** sort two-dimensional array key values ** @ param array $ arr two-dimensional array * @ param string $ keys key value * @ param string $ type ascending order: asc, descending order: desc (else) * @ return array */function Rray_sort ($ arr, $ keys, $ type = 'asc ') {$ keysvalue = $ new_array = array (); foreach ($ arr as $ k => $ v) {$ keysvalue [$ k] = $ v [$ keys];} if ($ type = 'asc ') {asort ($ keysvalue );} else {arsort ($ keysvalue);} reset ($ keysvalue); foreach ($ keysvalue as $ k => $ v) {$ new_array [$ k] = $ arr [$ k];} return $ new_array ;} /*** get client ip * @ param number $ type * @ return string */function get_client_ip ($ type = 0) {$ type = $ type? 1: 0; static $ ip = NULL; if ($ ip! = NULL) return $ ip [$ type]; if (isset ($ _ SERVER ['http _ X_FORWARDED_FOR ']) {$ arr = explode (',', $ _ SERVER ['http _ X_FORWARDED_FOR ']); $ pos = array_search ('Unknown', $ arr); if (false! ==$ Pos) unset ($ arr [$ pos]); $ ip = trim ($ arr [0]);} elseif (isset ($ _ SERVER ['http _ CLIENT_IP ']) {$ ip = $ _ SERVER ['http _ CLIENT_IP'];} elseif (isset ($ _ SERVER ['remote _ ADDR ']) {$ ip = $ _ SERVER ['remote _ ADDR'];} // valid ip address verification $ long = sprintf ("% u", ip2long ($ ip); $ ip = $ long? Array ($ ip, $ long): array ('0. 0.0.0 ', 0); return $ ip [$ type];} /*** convert a string to a camper name ** @ param string $ str * @ param boolean $ big * true false @ * @ return string */function hump_type ($ str, $ big = false) {$ str = strtolower ($ str); $ big and ucfirst ($ str ); $ str = preg_replace ("/_ ([a-zA-Z])/e", "strtoupper ('\ 1')", $ str ); return $ str;}/*** traverse and delete a folder ** @ param string $ path * folder address * @ return boolean */Function delete_files ($ path) {if (is_file ($ path) {return unlink ($ path);} if (is_dir ($ path )) {$ handle = opendir ($ path); if ($ handle! = False) {while (false! ==( $ File = readdir ($ handle) {if (in_array ($ file, array ('. ','.. ') continue; $ file = $ path. '/'. $ file; if (is_dir ($ file) {delete_files ($ file);} else if (is_file ($ file) {if (unlink ($ file) = false) return false ;}} closedir ($ handle) ;}return true ;}/ *** truncates a string (utf8 can be truncated) ** @ param string $ str * @ param int $ start * @ param int $ length * @ param string $ trim * @ param string $ cha Rset * @ return string */function sub ($ str, $ start, $ length, $ trim = "... ", $ charset = 'utf-8') {$ length + = 2; if (function_exists ('MB _ get_info ') {$ iLength = mb_strlen ($ str, $ charset); $ str = mb_substr ($ str, $ start, $ length, $ charset); if ($ length <$ iLength-$ start) {$ length-= 2; return mb_substr ($ str, $ start, $ length, $ charset ). $ trim;} else {return $ str;} else {preg_match_all ("/[\ x01-\ x7f] | [\ Xc2-\ xdf] [\ x80-\ xbf] | \ xe0 [\ xa0-\ xbf] [\ x80-\ xbf] | [\ xe1-\ xef] [\ x80-\ xbf] [\ x80-\ xbf] | \ xf0 [\ x90-\ xbf] [\ x80-\ xbf] [\ x80-\ xbf] | [\ xf1 -\ xf7] [\ x80-\ xbf] [\ x80-\ xbf] [\ x80-\ xbf]/", $ str, $ info); $ str = join ("", array_slice ($ info [0], $ start, $ length )); return ($ length <(sizeof ($ info [0])-$ start ))? $ Str. $ trim: $ str ;}}/*** UI friendly output * wireless parameter */function dump () {$ data = func_get_args (); ob_start (); foreach ($ data as $ v) {var_dump ($ v) ;}$ output = ob_get_clean (); if (! Extension_loaded ('xdebug') {$ output = preg_replace ('/\] \=\> \ n (\ s +)/M','] => ', $ output); $ output ='
' . htmlspecialchars ( $output, ENT_QUOTES ) . '
';} Echo ($ output);}/*** query the substrings on the left and delete the substrings if they are found, delete only once ** @ param string $ str * @ param string $ find * @ return string */function substr_left_once ($ str, $ find) {$ start = strpos ($ str, $ find); if (is_bool ($ start) {return $ str;} else {return substr ($ str, $ start + strlen ($ find ), strlen ($ str) ;}}/*** query the substring from the right side, and delete the substring if it is found, delete only once ** @ param string $ str * @ param string $ find * @ return string */function substr_right_once ($ str, $ find) {$ end = strrpos ($ str, $ find); if (is_bool ($ end) {return $ str;} else {return substr ($ str, 0, $ end );}}

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.