PHP Common function Code snippet (16~20) that can be used directly
Article Source: jquery tutorial?-? http://www.jq-school.com/Show.aspx?id=328
?
I've already shared the previous
1, PHP common function code fragment (the)
2. PHP Common function Code snippet (6~10)
3. PHP Common function Code snippet (11~15)
today is the 4th chapter, mainly to intercept the string, calculate how many weeks a year, return one weeks start time and end time (optional return timestamp or date), implement recursive create multilevel directory, recursive delete directory, detect QQ status and other functions, that is, 16th to 20th of these 5 useful code snippets, Hope can help The members of the jquery School group and the vast number of users of PHP Development to improve the development efficiency, the following is the 4th article.
16, PHP implementation of the common method of intercepting strings
/** * to intercept a string * params $string the string to intercept * params $length: Reserved Length (number of characters) * Params $dot: Excess part shows **/function _cutstr ($string, $le Ngth, $dot = ' ... ') {if (strlen ($string) <= $length) {return $string; } $string = Str_replace (' & ', ' ' ', ' < ', ' > '), Array (' & ', ' "', ' < ', ' > '), $string); $strcut = "; $n = $tn = $noc = 0; while ($n < strlen ($string)) {$t = Ord ($string [$n]); if ($t = = 9 | | $t = = 10 | | (<= $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 (<= $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 (' & ', ' "', ' < ', ' > '), $strcut); return $strcut. $dot;}
17, PHP implementation calculation How many weeks a year, return one weeks start and end time (optional return timestamp or date)
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; Calculates the total number of weeks in a given year $date = new DateTime; $date->setisodate ($year, +); $weeks = max ($date->format ("W"); 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 number of weeks is less than ten if ($week <10) { $week = ' 0 '. $week; } When the week starts and ends the timestamp $timestamp [' start '] = Strtotime ($year. ' W '. $week); $timestamp [' end '] = Strtotime (' +1 week-1 Day ', $timestamp [' Start ']); When the week starts and ends $timeymd [' start '] = Date ("y-m-d", $timestamp [' Start ']); $timeymd [' end '] = Date ("y-m-d", $timestamp [' End ']); Returns the start timestamp return $timestamp; Returns the Date form //return $timeymd;}
18, PHP implementation of recursive creation of multi-level directory of the General method
/** * +--------------------------------------------------------------------* Description recursively create directory +------------------ --------------------------------------------------* @param string $dir requires an innovative catalog +------------------------------ --------------------------------------* @return Returns True +--------------------------------------------if the directory exists or is created successfully ------------------------* @author Gongwen +--------------------------------------------------------------------*/ 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 implementation of the common method of recursive delete directory
/** * +--------------------------------------------------------------------* Description recursively delete directory +------------------ --------------------------------------------------* @param string $dir The directory to be deleted +------------------------------ --------------------------------------* @return Returns True +--------------------------------------------if the directory does not exist or if the book is successfully removed ------------------------* @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 implementation of the common method of detecting QQ status
function Qq_status () { if (empty ($QQ)) $qq = 429590191; $url = ' http://wpa.qq.com/pa?p=2: '. $qq. ': "; $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 (' 429590191 '); Test whether QQ is online, return 1 = online, return 2 = offline, return 0 = Error if ($qqwp ==1) { echo ' QQ online ';} ElseIf ($qqwp ==2) { echo ' qq offline ';} else { echo ' went wrong ';}
?