9 Classic PHP snippet sharing, _php tutorial

Source: Internet
Author: User
Tags get ip php source code xpath

9 Classic PHP Code snippets to share,


First, check whether the message has been read

When you send an email, you may be wondering if the message has been read by the other person. Here's a very interesting piece of code that shows the actual date and time that the other IP address record was read.

Copy CodeThe 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 (a);
$grigio = Imagecolorallocate ($newimage, 255,255,255);
Imagejpeg ($newimage);
Imagedestroy ($newimage);
?>

Ii. extracting keywords from web pages

A great piece of code can easily extract keywords from a Web page.

Copy the Code code as follows:
$meta = get_meta_tags (' http://www.emoticode.net/');
$keywords = $meta [' keywords '];
Split keywords
$keywords = Explode (', ', $keywords);
Trim them
$keywords = Array_map (' Trim ', $keywords);
Remove Empty values
$keywords = Array_filter ($keywords);
Print_r ($keywords);

Third, find all links on the page

Using the DOM, you can easily crawl links from any page, with the following code examples:

Copy the Code code as follows:
$html = file_get_contents (' http://www.example.com ');
$dom = new DOMDocument ();
@ $dom->loadhtml ($html);
Grab all the page
$xpath = new Domxpath ($dom);
$hrefs = $xpath->evaluate ("/html/body//a");
for ($i = 0; $i < $hrefs->length; $i + +) {
$href = $hrefs->item ($i);
$url = $href->getattribute (' href ');
echo $url. '
';
}

Iv. auto-Convert URL, skip to Hyperlink

In WordPress, if you want to automatically convert the URL, jump to the hyperlink page, you can take advantage of the built-in function make_clickable () to do this. If you want to operate the program based on WordPress, then you can refer to the wp-includes/formatting.php source code.

Copy CodeThe 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 ($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 ($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 is 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 was 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;
}

V. Create a data URL

The data URL can be embedded directly into the HTML/CSS/JS to save a large number of HTTP requests. The following code makes it easy to create a data URL with $file.

Copy the Code code as follows:
function Data_uri ($file, $mime) {
$contents =file_get_contents ($file);
$base 64=base64_encode ($contents);
echo "Data: $mime; base64, $base 64";
}

Vi. Download & Save a remote image from the server

When you build a website, download a picture from a remote server and save it on your own server, which is often used. The code is as follows:

Copy the Code code as follows:
$image = file_get_contents (' http://www.url.com/image.jpg ');
File_put_contents ('/images/image.jpg ', $image); Where to save the image

Vii. Remove the Remove Microsoft Word HTML Tag

When you use Microsoft Word, you create many tags, such as font,span,style,class. These tags are very useful for word itself, but when you paste from Word to page, you will find a lot of useless tag. Therefore, the following code can help you remove all useless word HTML tags.

Copy the Code code 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]

Viii. Detection of Browser language
If you have multiple languages on your site, you can use this code as the default language to detect the browser language. The 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;
}

Ix. showing the number of Facebook fans
If your website or blog has an on-line Facebook page, you may want to know how many fans you have. This code will help you to see how many Facebook fans you have, and remember to add the snippet to your page ID on the second line.

Copy the Code code 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%20from%20page%20where%20page_id= ". $page _id.") Or Die ("a lot");
$fans = $xml->page->fan_count;
Echo $fans;
?>

The above 9 Super Practical classic PHP code is very easy to use, the small partners with the freedom to play, a little change can be used in their own projects.

http://www.bkjia.com/PHPjc/929669.html www.bkjia.com true http://www.bkjia.com/PHPjc/929669.html techarticle 9 Classic PHP snippet sharing, one, check whether the message has been read when you send an email, you may be wondering if the message was read by the other person. Here is a very interesting paragraph ...

  • Related Article

    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.