9-Segment PHP Practical code recommendation _php Tips

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

First, see if the message has been read

When you are sending a message, you may be wondering if the email 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 records the read.

Copy Code code 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);

?>

Second, extract keywords from the Web page

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

Copy 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);

Find all the links on the page

Using the DOM, you can easily crawl links from any page, as shown in the code example:

Copy Code code as follows:

$html = file_get_contents (' http://www.example.com ');

$dom = new DOMDocument ();
@ $dom->loadhtml ($html);

Grab the on 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. ' <br/> ';
}

Four, the automatic conversion URL, jump to the hyperlink

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

Copy Code code 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]. "<a href=\" $url \ "rel=\" nofollow\ "> $url </a>". $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]. "<a href=\" $dest \ "rel=\" nofollow\ "> $dest </a>". $ret;
}

function _MAKE_EMAIL_CLICKABLE_CB ($matches) {
$email = $matches [2]. '@' . $matches [3];
return $matches [1]. "<a href=\" mailto: $email \ "> $email </a>";
}

function Make_clickable ($ret) {
$ret = '. $ret;
In testing, using arrays This is found to is 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 isn't in a array because we need it to run last, for cleanup of accidental links within links
$ret = Preg_replace (<a ([^>]+?>|>)) <a [^>]+?> ([^>]+?) </a></a> #i "," $1$3</a> ", $ret);
$ret = Trim ($ret);
return $ret;
}

V. Create a data URL

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

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

Download & Save a remote picture 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 Code code as follows:

$image = file_get_contents (' yun_qi_img/image.jpg ');
File_put_contents ('/images/image.jpg ', $image); Where to save the image

Vii. removing remove Microsoft Word HTML Tag

When you use Microsoft Word, you will create many tags, such as font,span,style,class. These tags are very useful for word itself, but when you paste it into a Web page from Word, you'll find a lot of unwanted tag. So the following code can help you delete all useless word HTML tags.

Copy Code code as follows:

function cleanhtml ($html) {
<summary>
Removes all FONT and SPAN tags, and all Class and Style attributes.
Designed to get rid of non-standard Microsoft Word HTML tags.
</summary>
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
}

Eight, the detection browser language

If you have multiple languages on your site, you can use this code as the default language to detect the browser language. This code will return to the initial language used by the browser client.

Copy Code code as follows:

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

Show the number of Facebook fans

If your site or blog has an inline Facebook page, you might want to know how many fans you have. This code will help you check the number of Facebook fans, remember, and don't forget to add the code to the second line of your page ID.

Copy 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;
?>

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.