Share nine classic PHP code snippets. _ PHP Tutorial

Source: Internet
Author: User
Tags get ip
Share nine classic PHP code snippets ,. Share nine classic PHP code snippets. 1. check whether the email has been read. when you send an email, you may be wondering whether the email has been read by the recipient. Here are nine interesting PHP code snippets,

1. check whether the email has been read

When you send an email, you may want to know whether the email has been read by the recipient. Here is a very interesting code snippet that shows the actual date and time when the recipient's IP address records are read.

The code is as follows:


<?
Error_reporting (0 );
Header ("Content-Type: image/jpeg ");
// Get IP
If (! Empty ($ _ SERVER ['http _ CLIENT_IP '])
{
$ Ip = $ _ SERVER ['http _ CLIENT_IP '];
}
Elseif (! Empty ($ _ SERVER ['http _ X_FORWARDED_FOR '])
{
$ Ip = $ _ SERVER ['http _ X_FORWARDED_FOR '];
}
Else
{
$ Ip = $ _ SERVER ['remote _ ADDR '];
}
// Time
$ Actual_time = time ();
$ Actual_day = date ('Y. m. D', $ actual_time );
$ Actual_day_chart = date ('d/m/Y', $ actual_time );
$ Actual_hour = date ('H: I: S', $ actual_time );
// GET Browser
$ Browser = $ _ SERVER ['http _ USER_AGENT '];
// LOG
$ MyFile = "log.txt ";
$ Fh = fopen ($ myFile, 'A + ');
$ StringData = $ actual_day. ''. $ actual_hour.''. $ ip. ''. $ browser.''. "\ r \ n ";
Fwrite ($ fh, $ stringData );
Fclose ($ fh );
// Generate Image (Es. dimesion is 1x1)
$ Newimage = ImageCreate (1, 1 );
$ Grigio = ImageColorAllocate ($ newimage, 255,255,255 );
ImageJPEG ($ newimage );
ImageDestroy ($ newimage );
?>

2. extract keywords from webpages

A great code snippet can easily extract keywords from webpages.

The code is as follows:


$ Meta = get_meta_tags ('http: // www.emoticode.net /');
$ Keywords = $ meta ['keyword'];
// Split keywords
$ Keywords = explode (',', $ keywords );
// Trim them
$ Keywords = array_map ('trim', $ keywords );
// Remove empty values
$ Keywords = array_filter ($ keywords );
Print_r ($ keywords );

3. search for all links on the page

With DOM, you can easily capture links from any page. the sample code is as follows:

The code is as follows:


$ Html = file_get_contents ('http: // www.example.com ');
$ Dom = new DOMDocument ();
@ $ Dom-> loadHTML ($ html );
// Grab all the on the page
$ Xpath = new DOMXPath ($ dom );
$ Hrefs = $ xpath-> evaluate ("/html/body // ");
For ($ I = 0; $ I <$ hrefs-> length; $ I ++ ){
$ Href = $ hrefs-> item ($ I );
$ Url = $ href-> getAttribute ('href ');
Echo $ url .'
';
}

4. automatically convert the URL and jump to the hyperlink

In WordPress, if you want to automatically convert the URL and jump to the hyperlink page, you can use the built-in function make_clickable () to perform this operation. If you want to operate this program outside of WordPress, you can refer to wp-nodes des/formatting. php source code.

The code is as follows:


Function _ make_url_clickable_cb ($ matches ){
$ Ret = '';
$ Url = $ matches [2];
If (empty ($ url ))
Return $ matches [0];
// Removed trailing [.,;:] from URL
If (in_array (substr ($ url,-1), array ('.', ';', ':') = true ){
$ Ret = substr ($ url,-1 );
$ Url = substr ($ url, 0, strlen ($ url)-1 );
}
Return $ matches [1]. "$ url". $ ret;
}
Function _ make_web_ftp_clickable_cb ($ matches ){
$ Ret = '';
$ Dest = $ matches [2];
$ Dest = 'http: // '. $ dest;
If (empty ($ dest ))
Return $ matches [0];
// Removed trailing [,;:] from URL
If (in_array (substr ($ dest,-1), array ('.', ';', ':') = true ){
$ Ret = substr ($ dest,-1 );
$ Dest = substr ($ dest, 0, strlen ($ dest)-1 );
}
Return $ matches [1]. "$ dest". $ ret;
}
Function _ make_email_clickable_cb ($ matches ){
$ Email = $ matches [2]. '@'. $ matches [3];
Return $ matches [1]. "$ email ";
}
Function make_clickable ($ ret ){
$ Ret = ''. $ ret;
// In testing, using arrays here was found to be faster
$ Ret = preg_replace_callback ('# ([\ s>]) ([\ w] +? : // [\ W \ x80-\ xff \ # $ % &~ /. \-;: = ,? @ \ [\] +] *) # Is ',' _ make_url_clickable_cb ', $ ret );
$ Ret = preg_replace_callback ('# ([\ s>]) (www | ftp) \. [\ w \ x80-\ xff \ # $ % &~ /. \-;: = ,? @ \ [\] +] *) # Is ',' _ make_web_ftp_clickable_cb ', $ ret );
$ Ret = preg_replace_callback ('# ([\ s>]) ([. 0-9a-z _ +-] +) @ ([0-9a-z-] + \.) + [0-9a-z] {2,}) # I ',' _ make_email_clickable_cb ', $ ret );
// This one is not in an array because we need it to run last, for cleanup of accidental links within links
$ Ret = preg_replace ("# (] +?> |>)] +?> ([^>] + ?) # I "," $1 $3 ", $ ret );
$ Ret = trim ($ ret );
Return $ ret;
}

5. create a data URL

Data URLs can be directly embedded into HTML, CSS, and JS to save a lot of HTTP requests. The following code allows you to easily create a data URL using $ file.

The code is as follows:


Function data_uri ($ file, $ mime ){
$ Contents = file_get_contents ($ file );
$ Base64 = base64_encode ($ contents );
Echo "data: $ mime; base64, $ base64 ";
}

6. download and save a remote image from the server

When you build a website, you can download an image from a remote server and save it on your own server. this operation is often used. The code is as follows:

The code is as follows:


$ Image = file_get_contents ('http: // www.url.com/image.jpg ');
File_put_contents ('/images/image.jpg', $ image); // Where to save the image

7. Remove the Remove Microsoft Word HTML Tag

When you use Microsoft Word, you will create many tags, such as font, span, style, and class. These tags are very useful for the Word itself, but when you paste the Word into a webpage, you will find a lot of useless tags. Therefore, the following code helps you delete all useless Word HTML tags.

The code is as follows:


Function cleanHTML ($ html ){
///


/// Removes all FONT and SPAN tags, and all Class and Style attributes.
/// Designed to get rid of non-standard Microsoft Word HTML tags.
///
// Start by completely removing all unwanted tags
$ Html = ereg_replace ("<(/)? (Font | span | del | ins) [^>] *> "," ", $ html );
// Then run another pass over the html (twice), removing unwanted attributes
$ Html = ereg_replace ("<([^>] *) (class | lang | style | size | face) = ("[^"] * "| '[^'] * '| [^>] +) ([^>] *)> ", "<\ 1>", $ html );
$ Html = ereg_replace ("<([^>] *) (class | lang | style | size | face) = ("[^"] * "| '[^'] * '| [^>] +) ([^>] *)> ", "<\ 1>", $ html );
Return $ html
}
[Code]

8. check the browser language
If your website has multiple languages, you can use this code as the default language to detect the browser language. This section of code returns the initial language used by the browser client.

[Code]
Function get_client_language ($ availableLanguages, $ default = 'en '){
If (isset ($ _ SERVER ['http _ ACCEPT_LANGUAGE ']) {
$ Langs = explode (',', $ _ SERVER ['http _ ACCEPT_LANGUAGE ']);

Foreach ($ langs as $ value ){
$ Choice = substr ($ value, 0, 2 );
If (in_array ($ choice, $ availableLanguages )){
Return $ choice;
}
}
}
Return $ default;
}

9. display the number of Facebook fans
If your website or blog has an internal link Facebook page, you may want to know how many fans you have. This code will help you check the number of Facebook fans. Remember, don't forget to add this code in the second line of your page ID.

The code is as follows:


<? Php
$ Page_id = "your page-ID ";
$ Xml = @ simplexml_load_file ("http://api.facebook.com/restserver.php? Method = facebook. fql. query & query = SELECT % 20fan_count % 20 FROM % 20 page % 20 WHERE % 20page_id = ". $ page_id. "") or die ("a lot ");
$ Fans = $ xml-> page-> fan_count;
Echo $ fans;
?>

The above nine super practical and classic PHP codes are very easy to use. you can use them in your own projects by making some modifications.

Warning: 1. check whether the email has been read. when you send an email, you may be wondering whether the email has been read by the recipient. This section is very interesting...

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.