10 Useful PHP Snippet _php Tutorials

Source: Internet
Author: User
Keyword highlighting
Copy CodeThe code is as follows:
function Highlight ($sString, $aWords) {
if (!is_array ($aWords) | | empty ($aWords) | |!is_string ($sString)) {
return false;
}
$sWords = implode (' | ', $aWords);
Return Preg_replace (' @\b ('. $sWords. ') \b@si ', ' $', $sString);
}

Get the users of your FeedBurner
Copy CodeThe code is as follows:
function Get_average_readers ($feed _id, $interval = 7) {
$today = Date (' y-m-d ', Strtotime ("Now");
$ago = Date (' y-m-d ', Strtotime ("-". $interval. "Days"));
$feed _url= "https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=". $feed _id. " &dates= ". $ago.", ". $today;
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_url, $feed _url);
$data = curl_exec ($ch);
Curl_close ($ch);
$xml = new SimpleXMLElement ($data);
$FB = $xml->feed->entry[' circulation ');
$NB = 0;
foreach ($xml->feed->children () as $circ) {
$NB + = $CIRC [' circulation '];
}
Return round ($NB/$interval);
}

Automatically generate passwords
Copy CodeThe code is as follows:
function Generatepassword ($length =9, $strength =0) {
$vowels = ' Aeuy ';
$consonants = ' Bdghjmnpqrstvz ';
if ($strength >= 1) {
$consonants. = ' Bdghjlmnpqrstvwxz ';
}
if ($strength >= 2) {
$vowels. = "Aeuy";
}
if ($strength >= 4) {
$consonants. = ' 23456789 ';
}
if ($strength >= 8) {
$vowels. = ' @#$% ';
}
$password = ";
$alt = time ()% 2;
for ($i = 0; $i < $length; $i + +) {
if ($alt = = 1) {
$password. = $consonants [(rand ()% strlen ($consonants))];
$alt = 0;
} else {
$password. = $vowels [(rand ()% strlen ($vowels))];
$alt = 1;
}
}
return $password;
}

Compress multiple CSS files
Copy CodeThe code is as follows:
Header (' content-type:text/css ');
Ob_start ("compress");
function Compress ($buffer) {
/* Remove Comments */
$buffer = Preg_replace ('!/\*[^*]*\*+ ([^/][^*]*\*+) */! ', ' ', $buffer);
/* Remove tabs, spaces, newlines, etc. */
$buffer = Str_replace ("r \ n", "\ r", "\ n", "\ T", "', '", "),", $buffer);
return $buffer;
}
/* Your CSS files */
Include (' master.css ');
Include (' typography.css ');
Include (' grid.css ');
Include (' print.css ');
Include (' handheld.css ');
Ob_end_flush ();

Get short URLs
Copy CodeThe code is as follows:
function Gettinyurl ($url) {
Return file_get_contents ("Http://tinyurl.com/api-create.php?url=". $url);
}

Calculate age based on birthday
Copy CodeThe code is as follows:
function Age ($date) {
$year _diff = ";
$time = Strtotime ($date);
if (FALSE = = = $time) {
Return ';
}
$date = Date (' y-m-d ', $time);
List ($year, $month, $day) = Explode ("-", $date);
$year _diff = Date ("Y") – $year;
$month _diff = Date ("M") – $month;
$day _diff = Date ("D") – $day;
if ($day _diff < 0 | | $month _diff < 0) $year _diff–;
return $year _diff;
}

Calculate Execution Time
Copy CodeThe code is as follows:
Create a variable for start time
$time _start = Microtime (true);
Place your php/html/javascript/css/etc.
Create a variable for end time
$time _end = Microtime (true);
Subtract The Times to get seconds
$time = $time _end-$time _start;
Echo ' Script took '. $time. ' Seconds to execute ';

Maintenance Mode for PHP
Copy CodeThe code is as follows:
function maintenance ($mode = FALSE) {
if ($mode) {
if (basename ($_server[' script_filename '))! = ' maintenance.php ') {
Header ("location:http://example.com/maintenance.php");
Exit
}
}else{
if (basename ($_server[' script_filename ')) = = = ' maintenance.php ') {
Header ("location:http://example.com/");
Exit
}
}
}

Prevent CSS styles from being cached
Copy CodeThe code is as follows:


Add ST\ND\RD to numbers, etc.
Copy CodeThe code is as follows:
function make_ranked ($rank) {
$last = substr ($rank,-1);
$seclast = substr ($rank,-2,-1);
if ($last > 3 | | $last = = 0) $ext = ' th ';
else if ($last = = 3) $ext = ' rd ';
else if ($last = = 2) $ext = ' nd ';
else $ext = ' st ';
if ($last = = 1 && $seclast = = 1) $ext = ' th ';
if ($last = = 2 && $seclast = = 1) $ext = ' th ';
if ($last = = 3 && $seclast = = 1) $ext = ' th ';
return $rank. $ext;
}

http://www.bkjia.com/PHPjc/324314.html www.bkjia.com true http://www.bkjia.com/PHPjc/324314.html techarticle keyword highlighting copy code is as follows: function Highlight ($sString, $aWords) {if (!is_array ($aWords) | | | empty ($aWords) | |!is_string ($sS Tring) {return false;} $sWords = i ...

  • 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.