These codes are often used in Web development. they are more practical and increase development efficiency. 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 ','
$1', $ SString );
}
Get your Feedburner user
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 password
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. = '20140901 ';
}
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;
}
Get short URL
Copy codeThe code is as follows:
Function getTinyUrl ($ url ){
Return file_get_contents ("http://tinyurl.com/api-create.php? Url = ". $ url );
}
Calculate the age based on the 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;
}
Computing 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. Here
// Create a variable for end time
$ Time_end = microtime (true );
// Subtract the two times to get seconds
$ Time = $ time_end-$ time_start;
Echo 'script took'. $ time. 'seconds to execute ';
PHP maintenance mode
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;
}
}
}
Prevents CSS styles from being cached
Copy codeThe code is as follows:
Add st \ nd \ rd and other numbers
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;
}
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.