Common PHP function code snippets that can be used directly (16 ~ 20) Source: jquery tutorial? -? Http://www.jq-school.com/Show.aspx? Id = 328? I have already shared Part 1. common PHP function code snippets (1 ~ 5) 2. common functional code snippets (16 ~ 20)
Source: jquery tutorial? -? Http://www.jq-school.com/Show.aspx? Id = 328
?
I have already shared it.
1. common PHP function code snippets (1 ~ 5)
2. common PHP function code snippets (6 ~ 10)
3. common PHP function code snippets (11 ~ 15)
Today is 4th articles, mainly including string truncation, calculation of the number of weeks in a year, return the start time and end time of a week (optional return the timestamp or date) to recursively create multi-level directories, recursively delete directories, and detect QQ status, that is, the 5 practical code snippets from 16th to 20.JqueryMembers of the school group andPHPDeveloped netizens improve development efficiency. The following are 4th articles.
16. common methods for intercepting strings in PHP
/*** Truncated string ** params $ string * params $ length: reserved length (number of characters) * params $ dot: display the remaining part **/function _ cutstr ($ string, $ length, $ dot = '... ') {if (strlen ($ string) <= $ length) {return $ string;} $ string = str_replace (array (' & ',' "',' <', '>'), array ('&', '"', '<', '>'), $ string); $ strcut = ''; $ n = $ tn = $ noc = 0; while ($ n <strlen ($ string) {$ t = ord ($ string [$ n]); if ($ t = 9 | $ t = 10 | (32 <= $ t & $ t <= 126) {$ tn = 1; $ n ++; $ noc ++;} elseif (194 <=$ t & $ t <= 223) {$ tn = 2; $ n + = 2; $ noc + = 2;} elseif (224 <= $ t & $ t <239) {$ tn = 3; $ n + = 3; $ noc + = 2 ;} elseif (240 <= $ t & $ t <= 247) {$ tn = 4; $ n + = 4; $ noc + = 2 ;} elseif (248 <= $ t & $ t <= 251) {$ tn = 5; $ n + = 5; $ noc + = 2 ;} elseif ($ t = 252 | $ t = 253) {$ tn = 6; $ n + = 6; $ noc + = 2 ;} else {$ n ++;} if ($ noc >=$ length) {break;} if ($ noc >$ length) {$ n-= $ tn ;} $ strcut = substr ($ string, 0, $ n); $ strcut = str_replace (array ('&', '"', '<', '> '), array ('&', '"', '<', '>'), $ strcut); return $ strcut. $ dot ;}
17. PHP calculates the number of weeks in a year, and returns the start time and end time of a week (optional)
Function getWeekStartAndEnd ($ year, $ week = 1) {header ("Content-type: text/html; charset = utf-8"); date_default_timezone_set ("Asia/Shanghai "); $ year = (int) $ year; $ week = (int) $ week; // calculate the total number of weeks of the current year by the given year $ date = new DateTime; $ date-> setISODate ($ year, 53); $ weeks = max ($ date-> format ("W"), 52 ); // if the given number of weeks is greater than the total number of weeks or less than or equal to 0 if ($ week> $ weeks | $ week <= 0) {return false ;} // if the week number is less than 10 if ($ week <10) {$ week = '0 '. $ week;} // the start and end timestamp of the week $ timestamp ['start'] = strtotime ($ year. 'W '. $ week); $ timestamp ['end'] = strtotime ('+ 1 week-1 Day', $ timestamp ['start']); // start date of the week $ timeymd ['start'] = date ("Y-m-d", $ timestamp ['start']); $ timeymd ['end'] = date ("Y-m-d", $ timestamp ['end']); // return the start timestamp return $ timestamp; // return date format // return $ timeymd ;}
18. PHP provides a general method to recursively create multi-level directories
/*** + Catalog * Description recursively create Directory + ------------------------------------------------------------------------ * @ param string $ dir the directory to be innovated + catalog * @ return if the directory exists, if the creation is successful, TRUE + ---------------------------------------------------------------------- * @ author gongwen + ---------------- is returned ---------------- ------------------------------------------------ */Function mkdirs ($ dir, $ mode = 0777) {if (is_dir ($ dir) | mkdir ($ dir, $ mode) return TRUE; if (! Mkdirs (dirname ($ dir), $ mode) return FALSE; return mkdir ($ dir, $ mode );}
19. PHP Generic method for recursively deleting directories
/*** + Delete * Description recursively delete directories + directories * @ param string $ dir directory to be deleted + directories * @ return if the directory does not exist or the volume is successfully deleted, TRUE + ------------------------------------------------------------------ * @ author gongwen + ---------------- -------------------------------------------------- */Function rmdirs ($ dir) {if (! Is_dir ($ dir) | rmdir ($ dir) return TRUE; if ($ dir_handle = opendir ($ dir) {while ($ filename = readdir ($ dir_handle )) {if ($ filename! = '.' & $ Filename! = '..') {$ SubFile = $ dir. '/'. $ filename;} is_dir ($ subFile )? Rmdirs ($ subFile): unlink ($ subFile);} closedir ($ dir_handle); return rmdir ($ dir );}}
20. PHP universal method for detecting QQ status
Function qq_status () {if (empty ($ qq) $ qq = 429590191; $ url = 'http: // wpa.qq.com/pa? P = 2 :'. $ qq. ': 52'; $ Headers = get_headers ($ url, 1); if ($ Headers ['location'] = 'http: // pub.idqqimg.com/qconn/wpa/button/button_121.gif') {$ Status = 1; // QQ online} elseif ($ Headers ['location'] = 'http: // pub.idqqimg.com/qconn/wpa/button/button_120.gif') {$ Status = 2; // QQ offline} else {$ Status = 0; // Unknown} Return $ Status;} // Test Data $ qqwp = qq_status ('20140901 '); // test whether QQ is online. 1 = online is returned, 2 = offline is returned, and 0 = error if ($ qqwp = 1) {echo 'qq Online ';} elseif ($ qqwp = 2) {echo 'qq offline';} else {echo 'error ';}
?