9 Practical PHP Code Snippets sharing _php Tips

Source: Internet
Author: User
Tags explode get ip xpath

A To see if a message has been read

When you send a message, you must be wondering if your email has been viewed by the other person. The following code enables you to read the IP address of your email, as well as the actual reading date and time.

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

Source code: http://www.emoticode.net/php/code-to-find-out-if-your-email-has-been-read.html

two. Extract keywords from a Web page

This excellent code can simply implement the ability to 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);

Source code: http://www.emoticode.net/php/extract-keywords-from-any-webpage.html

three. Find all links on a page

Using DOM, you can crawl links on any page, such as below.

Copy Code code as follows:

$html = file_get_contents (' http://www.php100.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. ';
}

Source code: http://snipplr.com/view/70489/find-all-links-on-a-page/

Four. Automatically convert URLs to clickable hyperlinks

In WordPress, if you want to automatically convert all URLs to clickable hyperlinks, you can use the built-in function make_clickable () implementation. When you operate outside of WordPress, you can refer to the source code in wp-includes/formatting.php.
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]. "$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 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 ("# (]+?>|>))]+?>" ([^>]+?) #i "," $1$3 ", $ret);
$ret = Trim ($ret);
return $ret;
}

Source code: http://zenverse.net/php-function-to-auto-convert-url-into-hyperlink/

Five. To create a data URI

The data URI can help in embedding images into HTML/CSS/JS, thereby saving HTTP requests. The following function can use $file to create a data URI.

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

Source code: http://css-tricks.com/snippets/php/create-data-uris/

Six. Download and save remote pictures to your server

When you're building a site, it's likely that you'll download images from a remote server to your own server, and the following code will help you implement this functionality.
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

Source code: http://www.catswhocode.com/blog/snippets/download-save-a-remote-image-on-your-server-using-php

Seven. Remove Microsoft Word HTML tags

When you use Microsoft Word, you create a lot of tag tags, such as font, span, style, class, and so on, which are useful in word, but when you paste text from Word into a Web page, there are a lot of useless tags. The following useful functions can help you clear all the word HTML tags.
Copy 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
}

Source code: Http://tim.mackey.ie/CommentView,guid,2ece42de-a334-4fd0-8f94-53c6602d5718.aspx

Eight. Detecting Browser Language

If your site is multilingual, the following code can help you detect the browser language, and it will return to the default language of the client browser.

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

Source code: http://snipplr.com/view/12631/detect-browser-language/

Nine. Show the number of fans on Facebook

If you have a Facebook page in your website or blog, you may want to show the number of fans on Facebook, and the following code will help you get the number of fans, and don't forget to add your page ID to the second line of code.
Copy Code code as follows:

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

Source code: Http://www.wprecipes.com/display-number-of-facebook-fans-in-full-text-on-your-wordpress-blog

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.