Common php Functions

Source: Internet
Author: User

Common php Functions
/*** Common php functions/1. encryption and decryption string function encryptDecrypt ($ key, $ string, $ decrypt) {if ($ decrypt) {$ decrypted = rtrim (mcrypt_decrypt (MCRYPT_RIJNDAEL_256, md5 ($ key), base64_decode ($ string), MCRYPT_MODE_CBC, md5 (md5 ($ key), "12 "); return $ decrypted;} else {$ encrypted = base64_encode (mcrypt_encrypt (encrypt, md5 ($ key), $ string, MCRYPT_MODE_CBC, md5 ($ key )))); return $ encrypted ;}}// The following is the string "Helloweb A. Welcome to "encrypt and decrypt respectively. // encryption: echo encryptDecrypt ('Password', 'helloweba, Welcome ', 0); // decryption: echo encryptDecrypt ('Password ', 'z0jax4qmwcf + db5TNbp/xwdUM84snRsXvvpXuaCa4Bk = ', 1); 2. function generateRandomString ($ length = 10) {$ characters = 'signature '; $ randomString = ''; for ($ I = 0; $ I <$ length; $ I ++) {$ randomString. = $ characters [rand (0, strle N ($ characters)-1)];} return $ randomString;} 3. php obtains the file suffix function getExtension ($ filename) {$ myext = substr ($ filename, strrpos ($ filename ,'. '); return str_replace ('. ', '', $ myext);} 4. php obtains the file size and formats the following functions to obtain the file size, and converts it to a readable format such as KB and MB. Function formatSize ($ size) {$ sizes = array ("Bytes", "KB", "MB", "GB", "TB", "PB", "EB ", "ZB", "YB"); if ($ size = 0) {return ('n'/');} else {return (round ($ size/pow (1024, ($ I = floor (log ($ size, 1024), 2 ). $ sizes [$ I]) ;}/// $ thefile = filesize('test_file}'); 5. Replace the character tag function stringParser with php ($ string, $ replacer) {$ result = str_replace (array_keys ($ replacer), array_values ($ replacer), $ s Tring); return $ result ;} $ string = 'the {B} anchor text {/B} is The {B} actual word {/B} or words used {br} to describe the link {br} itself'; $ replace_array = array ('{B}' => '<B>', '{/B}' => '</B> ', '{br}' => '<br/>'); echo stringParser ($ string, $ replace_array); 6. php lists all file names in the folder. function listDirFiles ($ DirPath) {if ($ dir = opendir ($ DirPath) {while ($ file = readdir ($ dir ))! = False) {if (! Is_dir ($ DirPath. $ file) {echo "filename: $ file <br/>" ;}}}// listDirFiles ('home/some_folder /'); 7. php retrieves the urlfunction curPageURL () {$ pageURL = 'http'; if (! Empty ($ _ SERVER ['https']) {$ pageURL. = "s" ;}$ pageURL. = ": //"; if ($ _ SERVER ["SERVER_PORT"]! = "80") {$ pageURL. = $ _ SERVER ["SERVER_NAME"]. ":". $ _ SERVER ["SERVER_PORT"]. $ _ SERVER ["REQUEST_URI"];} else {$ pageURL. = $ _ SERVER ["SERVER_NAME"]. $ _ SERVER ["REQUEST_URI"];} return $ pageURL;} echo curPageURL (); 8. php forcibly downloads the file function download ($ filename) {if (isset ($ filename) & (file_exists ($ filename) {header ("Content-length :". filesize ($ filename); header ('content-Type: application/octet-stream'); hea Der ('content-Disposition: attachment; filename = "'. $ filename. '"'); readfile (" $ filename ");} else {echo" Looks like file does not exist! ";}} Download ('/down/test_45f73e852.zip'); 9. Use '... 'display/* Utf-8, gb2312 are supported by the Chinese character truncation function cut_str (string, truncation length, start length, encoding ); the default encoding start length is UTF-8. The default value is 0 */function cutStr ($ string, $ sublen, $ start = 0, $ code = 'utf-8 ') {if ($ code = 'utf-8 ') {$ pa = "/[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]/"; preg_match _ All ($ pa, $ string, $ t_string); if (count ($ t_string [0])-$ start> $ sublen) return join ('', array_slice ($ t_string [0], $ start, $ sublen )). "... "; return join ('', array_slice ($ t_string [0], $ start, $ sublen);} else {$ start = $ start * 2; $ sublen = $ sublen * 2; $ strlen = strlen ($ string); $ tmpstr = ''; for ($ I = 0; $ I <$ strlen; $ I ++) {if ($ I >=$ start & $ I <($ start + $ sublen) {if (ord (substr ($ string, $ I, 1)> 129) {$ tmps Tr. = substr ($ string, $ I, 2);} else {$ tmpstr. = substr ($ string, $ I, 1) ;}if (ord (substr ($ string, $ I, 1)> 129) $ I ++ ;} if (strlen ($ tmpstr) <$ strlen) $ tmpstr. = "... "; return $ tmpstr ;}$ str =" loading images and page effects implemented by the jQuery plug-in "; echo cutStr ($ str, 16 ); 10. PHP obtains the client's real IP address // obtains the user's real IPfunction getIp () {if (getenv ("HTTP_CLIENT_IP") & strcasecmp (getenv ("HTTP_CLIENT_IP "), "unknown") $ ip = getenv ("HTTP_CLIENT_IP"); else if (Getenv ("HTTP_X_FORWARDED_FOR") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown") $ ip = getenv ("HTTP_X_FORWARDED_FOR "); else if (getenv ("REMOTE_ADDR") & strcasecmp (getenv ("REMOTE_ADDR"), "unknown") $ ip = getenv ("REMOTE_ADDR "); else if (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], "unknown ")) $ ip = $ _ SERVER ['remote _ ADDR ']; else $ ip = "un Known "; return ($ ip) ;}echo getIp (); 11. Random color generator function rabdowColor () {$ rand = array ('0', '1 ', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B ', 'C', 'D', 'E', 'F'); $ color = '#'. $ rand [rand (0, 15)]. $ rand [rand (0, 15)]. $ rand [rand (0, 15)]. $ rand [rand (0, 15)]. $ rand [rand (0, 15)]. $ rand [rand ()]; return $ color;} // $ color = rabdowColor (); 12. Two-dimensional array deduplication function unique ($ data = array ()) {$ tmp = array (); foreach ($ d Ata as $ key => $ value) {// combines the one-dimensional array key value with the key name foreach ($ value as $ key1 => $ value1) {$ value [$ key1] = $ key1. '_ | _'. $ value1; // _ | _ delimiter complexity to avoid conflict} $ tmp [$ key] = implode (', |,', $ value); //, |, complex separators to avoid conflicts} // repeat the array after Dimensionality Reduction $ tmp = array_unique ($ tmp); // reorganize the two-dimensional array $ newArr = array (); foreach ($ tmp as $ k => $ tmp_v) {$ tmp_v2 = explode (', |,', $ tmp_v); foreach ($ tmp_v2 as $ k2 => $ v2) {$ v2 = explode ('_ | _', $ v2); $ tmp_v3 [$ v2 [0] = $ V2 [1] ;}$ newArr [$ k] = $ tmp_v3;} return $ newArr ;}$ newArr = unique ($ list ); 13. function strCutByStr (& $ str, $ findStart, $ findEnd = false, $ encoding = 'utf-8 ') {if (is_array ($ findStart) {if (count ($ findStart) === count ($ findEnd) {foreach ($ findStart as $ k => $ v) {if ($ result = strCutByStr ($ str, $ v, $ findEnd [$ k], $ encoding ))! = False) {return $ result;} return false;} else {return false;} if ($ start = mb_strpos ($ str, $ findStart, 0, $ encoding) ==== false) {return false;} $ start + = mb_strlen ($ findStart, $ encoding); if ($ findEnd = false) {return mb_substr ($ str, $ start, NULL, $ encoding);} if ($ length = mb_strpos ($ str, $ findEnd, $ start, $ encoding )) ===false) {return false;} return mb_substr ($ str, $ start, $ length- $ Start, $ encoding);} $ str = '1 is a game. 23. I want to ask "4567890"; echo (strCutByStr ($ str, 'That is', 'stair ')); // output a field 23. I will ask 4567890 if the content is online. If there is any infringement, please contact Delete. Thank you.

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.