1. Keyword highlighting
| The code is as follows: |
Copy code |
|
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',' <strong style = "background-color: yellow"> $1 </strong> ', $ sString );
} |
2. Get your Feedburner user
| The code is as follows: |
Copy code |
|
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 );
} |
3. Automatically generate a password
| The code is as follows: |
Copy code |
|
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 ';
} Www, 111cn.net
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;
} |
4. Compress multiple CSS files
| The code is as follows: |
Copy code |
|
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 ("rn", "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 (); |
5. Get the short URL
| The code is as follows: |
Copy code |
Function getTinyUrl ($ url ){
Return file_get_contents ("http://tinyurl.com/api-create.php? Url = ". $ url );
} |
6. Calculate the age based on the birthday
| The code is as follows: |
Copy code |
|
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;
} |
7. Calculate the execution time
| The code is as follows: |
Copy code |
|
// 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 '; 8. PHP maintenance mode
Function maintenance ($ mode = FALSE ){
If ($ mode) {www.111cn.net
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;
}
}
} |
9. Prevent CSS styles from being cached
| The code is as follows: |
Copy code |
|
<Link href = "/stylesheet.css? <? Php echo time () ;?> "Rel =" stylesheet "type =" text/css "/& glt; 10. Increase the number by stndrc.
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;
} |
Identify Source by IP address
This is a very practical code snippet that helps you identify visitor sources through IP addresses. The following method receives a parameter and returns the IP address location. If not found, return UNKNOWN.
| The code is as follows: |
Copy code |
Function detect_city ($ ip ){
$ Default = 'unknown ';
If (! Is_string ($ ip) | strlen ($ ip) <1 | $ ip = '192. 0.0.1 '| $ ip = 'localhost ')
$ Ip = '8. 8.8.8 ';
$ Curlopt_useragent = 'mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: 1.9.2) Gecko/20100115 Firefox/3.6 (. net clr 3.5.30729 )';
$ Url = 'http: // ipinfodb.com/ip_locator.php? Ip = '. urlencode ($ ip );
$ Ch = curl_init ();
$ Curl_opt = array (
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERAGENT => $ curlopt_useragent,
CURLOPT_URL => $ url,
CURLOPT_TIMEOUT => 1,
CURLOPT_REFERER => 'http: // '. $ _ SERVER ['http _ host'],
);
Curl_setopt_array ($ ch, $ curl_opt );
$ Content = curl_exec ($ ch );
If (! Is_null ($ curl_info )){
$ Curl_info = curl_getinfo ($ ch );
}
Curl_close ($ ch );
If (preg_match ('{<li> City: ([^ <] *) </li>} I', $ content, $ regs )){
$ City = $ regs [1];
}
If (preg_match ('{<li> State/Province: ([^ <] *) </li>} I', $ content, $ regs )){
$ State = $ regs [1];
}
If ($ city! = ''& $ State! = ''){
$ Location = $ city. ','. $ state;
Return $ location;
} Else {
Return $ default;
}
} |
Determine the dominant color of an image
The following code is very practical and can help you determine the dominant color of an image. You can analyze any image.
| The code is as follows: |
Copy code |
$ I = imagecreatefromjpeg ("image.jpg ");
For ($ x = 0; $ x <imagesx ($ I); $ x ++ ){
For ($ y = 0; $ y <imagesy ($ I); $ y ++ ){
$ Rgb = imagecolorat ($ I, $ x, $ y );
$ R = ($ rgb> 16) & 0xFF;
$ G = ($ rgb >>& 0xFF;
$ B = $ rgb & 0xFF;
$ RTotal + = $ r;
$ GTotal + = $ g;
$ BTotal + = $ B;
$ Total ++;
}
}
$ RAverage = round ($ rTotal/$ total );
$ GAverage = round ($ gTotal/$ total );
$ BAverage = round ($ bTotal/$ total ); |
Emails instead of PHP errors are not displayed
If you do not want to display a PHP error on the page, you can also get the error message by email. The following code helps you implement it.
| The code is as follows: |
Copy code |
<? Php
// Our custom error handler
Function nettuts_error_handler ($ number, $ message, $ file, $ line, $ vars ){
$ Email ="
<P> An error ($ number) occurred on line
<Strong> $ line </strong> and in the <strong> file: $ file. </strong>
<P> $ message </p> ";
$ Email. = "<pre>". print_r ($ vars, 1). "</pre> ";
$ Headers = 'content-type: text/html; charset = ISO-8859-1 '. "rn ";
// Email the error to someone...
Error_log ($ email, 1, 'you @ youremail.com', $ headers );
// Make sure that you decide how to respond to errors (on the user's side)
// Either echo an error message, or kill the entire project. Up to you...
// The code below ensures that we only "die" if the error was more
// Just a NOTICE.
If ($ number! ==E_notice) & ($ number <2048 )){
Die ("There was an error. Please try again later .");
}
}
// We shoshould use our custom function to handle errors.
Set_error_handler ('nettuts _ error_handler ');
// Trigger an error... (var doesn't exist)
Echo $ somevarthatdoesnotexist; |