Previously, the research and Development channel has released the "direct to use, 10 PHP code Snippets," The Netizen has been unanimously praised. In this article, I will continue to share nine super useful PHP code snippets. Using this code can save you a lot of time when you're developing a website, app, or blog.
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.
Source
Ii. extracting keywords from web pages
A great piece of code can easily extract keywords from a Web page.
$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 ($k Eywords);p Rint_r ($keywords);
Source
Third, find all links on the page
Using the DOM, you can easily crawl links from any page, with the following code examples:
$html = file_get_contents (' http://www.bkjia.com '); $dom = new DOMDocument (); @ $dom->loadhtml ($html);//Grab all 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
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.
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 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;}Source
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.
function Data_uri ($file, $mime) { $contents =file_get_contents ($file); $base 64=base64_encode ($contents); echo "Data: $mime; base64, $base 64";}
Source
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:
$image = file_get_contents (' http://www.bkjia.com/image.jpg '); file_put_contents ('/images/image.jpg ', $image);// Where to save the image
Source
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.
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$ht ml = Ereg_replace ("< ([^>]*) (Class|lang|style|size|face) = (" [^ "]*" | " [^']*'| [^>]+] ([^>]*) > "," <\1> ", $html); $html = Ereg_replace (" < ([^>]*) (Class|lang|style|size|face) = (" [^"]*"|' [^']*'| [^>]+] ([^>]*) > "," <\1> ", $html); return $html}
Source
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.
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
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.
page->fan_count; Echo $fans;? >
Source
English: Catswhocode
http://www.bkjia.com/PHPjc/625805.html www.bkjia.com true http://www.bkjia.com/PHPjc/625805.html techarticle Previously, the research and Development channel has released the "direct to use, 10 PHP code Snippets," The Netizen has been unanimously praised. In this article, I will continue to share nine super useful PHP code snippets. When ...