10 个精彩的PHP代码片段
jopen 12年前
<h2>1. Get Tomorrow Date:</h2> <pre class="brush:php; toolbar: true; auto-links: false;">function get_tomorrow_date() { $tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y")); return date("Y-m-d", $tomorrow); }</pre> <h2>2. Get Today's Date:</h2> <pre class="brush:php; toolbar: true; auto-links: false;">function get_today_date() { $today=date("Y-m-d"); return today; }</pre> <h2>3. Get Last Day Date:</h2> <pre class="brush:php; toolbar: true; auto-links: false;">function get_last_date() { $tomorrow = mktime(0,0,0,date("m"),date("d")-1,date("Y")); return date("Y-m-d", $tomorrow); }</pre> <h2>4. Detect Local Host:</h2> <pre class="brush:php; toolbar: true; auto-links: false;">function is_localhost() { $localhost_ids = array('localhost', '127.0.0.1'); if(in_array($_SERVER['HTTP_HOST'],$localhost_ids)){ // not valid return 1; } }</pre> <h2>5. Get Day Name By Date:</h2> <pre class="brush:php; toolbar: true; auto-links: false;">function get_day_name_by_date($day,$month, $year) { $day_name = date("l", mktime(0,0,0,$month,$day,$year)); return $day_name; }</pre> <h2>6. Print an array in nice way:</h2> <pre class="brush:php; toolbar: true; auto-links: false;">function debugPrint($object, $title = "", $isMarkup = false) { if( !empty($title)){ echo "$title: "; } if( $isMarkup == false){ echo "<pre>"; print_r($object); echo "</pre>"; } else{ echo htmlspecialchars($object); } }</pre> <h2>7. Get Month Name By Month Value:</h2> <pre class="brush:php; toolbar: true; auto-links: false;">function Month($NumMonth) { switch($NumMonth) { Case "01": return "January"; break; Case "02": return "February"; break; Case "03": return "March"; break; Case "04": return "April"; break; Case "05": return "May"; break; Case "06": return "June"; break; Case "07": return "July"; break; Case "08": return "August"; break; Case "09": return "September"; break; Case "10": return "October"; break; Case "11": return "November"; break; Case "12": return "December"; break; } }</pre> <h2>8. Replace/Remove Unwanted Texts/syntax from a string:</h2> <pre class="brush: php;fontsize: 100; first-line: 1; ">function setDescriptionString($inputString=""){ $replacedWords = array("…","‘","ó","<ul>","<li>","–","…"," ",""","&","“","”","<span>","</span>", "<p>","</p>", "/",":", "<", ">","<strong>", "</strong>", "<b>", "</b>", "<i>","</i>","span","strong","class","+","-","=","'","’"); $filteredString = str_replace($replacedWords, "", $inputString)."."; return $filteredString; }</pre> <h2>9. Get Extension of a file/image:</h2> <pre class="brush:php; toolbar: true; auto-links: false;">function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; }</pre> <h2>10. Remove Spacial Characters from a string:</h2> <pre class="brush:php; toolbar: true; auto-links: false;">function removeSpacialCharacters($string="") { if (preg_match('/[^\w\d_ -]/si', $string)) { return preg_replace('/[^a-zA-Z0-9_ -]/s', '', $string); } else { return preg_replace('/\s/', ' ', $string); } }</pre>