10 Practical PHP code snippets _ PHP Tutorial

Source: Internet
Author: User
10 Practical PHP code snippets. Author: aolinks keyword highlighting functionhighlight ($ sString, $ aWords) {if (! Is_array ($ aWords) | emptyempty ($ aWords) |! Is_string ($ sString) {returnfalse;} $ sWordsimplo author: aolinks
Keyword Highlighting

Function highlight ($ sString, $ aWords ){
If (! Is_array ($ aWords) | emptyempty ($ aWords) |! Is_string ($ sString )){
Return false;
}

$ SWords = implode ('|', $ aWords );
Return preg_replace ('@ \ B ('. $ sWords. ') \ B @ Si ','$1', $ SString );
}


Get your Feedburner user

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

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;
}

Compress multiple CSS files

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 (array ("\ 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 URL

Function getTinyUrl ($ url ){
Return file_get_contents ("http://tinyurl.com/api-create.php? Url = ". $ url );
}

Calculate the age based on the birthday

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

// 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

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

Add st \ nd \ rd and other numbers

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;
}

Function highlight ($ sString, $ aWords) {if (! Is_array ($ aWords) | emptyempty ($ aWords) |! Is_string ($ sString) {return false;} $ sWords = implo...

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.