Very practical PHP code snippets (highly recommended)_PHP tutorial

WBOY
Release: 2016-07-13 10:49:02
Original
904 people have browsed it

There are many PHP code snippets on the Internet that can improve development efficiency, and you can also learn the techniques and apply them in your own projects. Below, I Love Boiled Fish has selected a few more useful PHP snippets.

Extract keywords from web pages

Extract keywords from the specified page and display them.

The code is as follows Copy code
 代码如下 复制代码

$meta = get_meta_tags('http://www.bKjia.c0m/');
$keywords = $meta['keywords'];
// 分割关键词
$keywords = explode(',', $keywords );
// 整理
$keywords = array_map( 'trim', $keywords );
// 去掉空内容
$keywords = array_filter( $keywords );

print_r( $keywords );

$meta = get_meta_tags('http://www.bKjia.c0m/');

$keywords = $meta['keywords'];
// Split keywords

$keywords = explode(',', $keywords );
 代码如下 复制代码

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

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

$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.'
';
}

// Organize $keywords = array_map( 'trim', $keywords ); // Remove empty content $keywords = array_filter( $keywords ); print_r( $keywords );
Get all links on the page The following code can use PHP DOM to obtain all links in the specified page. It is only used as a starting point, and you can use it freely.
The code is as follows Copy code
$html = file_get_contents('http://www.hzhuti.com'); $dom = new DOMDocument(); @$dom->loadHTML($html); $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.'
'; }

Automatically convert URLs on the page into clickable hyperlinks
If you publish some articles or make some pages, if you want to put a hyperlink, you must write an a tag. Use the following code to easily convert URLs into hyperlink output. The implementation method is relatively simple. The general idea is to use regular expressions to match the URL and then process the output hyperlink.

The code is as follows Copy code
 代码如下 复制代码

function _make_url_clickable_cb($matches) {
 $ret = '';
 $url = $matches[2];
 
 if ( empty($url) )
  return $matches[0];
 // 去掉 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];
 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;
 $ret = preg_replace_callback('#([s>])([w]+?://[wx80-xff#$%&~/.-;:=,?@[]+]*)#is', '_make_url_clickable_cb', $ret);
 $ret = preg_replace_callback('#([s>])((www|ftp).[wx80-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);
 
 $ret = preg_replace("#(]+?>|>))]+?>([^>]+?)#i", "", $ret);
 $ret = trim($ret);
 return $ret;
}

function _make_url_clickable_cb($matches) {

$ret = '';
$url = $matches[2];

 代码如下 复制代码

function data_uri($file, $mime) {
  $contents=file_get_contents($file);
  $base64=base64_encode($contents);
  echo "data:$mime;base64,$base64";
}

if ( empty($url) )

Return $matches[0];
// Remove the punctuation marks after the URL

if ( in_array(substr($url, -1), array('.', ',', ';', ':')) === true ) {
 代码如下 复制代码
$image = file_get_contents('http://www.bKjia.c0m/logo.gif');
file_put_contents('/images/logo.gif', $image);
$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]; 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; $ret = preg_replace_callback('#([s>])([w]+?://[wx80-xff#$%&~/.-;:=,?@[]+]*)#is', '_make_url_clickable_cb', $ret); $ret = preg_replace_callback('#([s>])((www|ftp).[wx80-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); $ret = preg_replace("#(]+?>|>))]+?>([^>]+?)#i", "$1$3", $ret); $ret = trim($ret); return $ret; }
Generate Data URI code using PHP Images are usually encoded into Data URI format and used in web pages to reduce HTTP requests and improve front-end performance. There are also some other uses. The following code encodes a file into a Data URI.
The code is as follows Copy code
function data_uri($file, $mime) { $contents=file_get_contents($file); $base64=base64_encode($contents); echo "data:$mime;base64,$base64"; }
Download remote images to local server Especially for reprinting articles, etc., in order to prevent the other party's website from shutting down and causing the loss of images, the images from the remote server are usually downloaded to the local server when publishing the article. The following code simply implements this requirement. More storage locations and traversal links need to be customized by yourself:
The code is as follows Copy code
$image = file_get_contents('http:// www.bKjia.c0m/logo.gif'); file_put_contents('/images/logo.gif', $image);

Remove useless tags in the text
When copying text from some text editors (such as Word) to a web page editor, there may be some additional useless tags, such as style that specify text styles, etc. The following code can remove these useless tags and purify the text through regular matching:

function cleanHTML($html) {
The code is as follows
 代码如下 复制代码

function cleanHTML($html) {

// 首先去掉无用的标签(可以自定义更多需要清除的标签)

$html = ereg_replace("<(/)?(font|span|del|ins)[^>]*>","",$html);

// 然后再运行两遍去掉无用属性

$html = ereg_replace("<([^>]*)(class|lang|style|size|face)=("[^"]*"|'[^']*'|[^>]+)([^>]*)>","<1>",$html);
$html = ereg_replace("<([^>]*)(class|lang|style|size|face)=("[^"]*"|'[^']*'|[^>]+)([^>]*)>","<1>",$html);

return $html
}

Copy code

// First remove useless tags (you can customize more tags that need to be cleared)

$html = ereg_replace("<(/)?(font|span|del|ins)[^>]*>","",$html);

$html = ereg_replace("<([^>]*)(class|lang|style|size|face)=("[^"]*"|'[^']*'|[^ >]+)([^>]*)>","<1>",$html); $html = ereg_replace("<([^>]*)(class|lang|style|size|face)=("[^"]*"|'[^']*'|[^>] +)([^>]*)>","<1>",$html); return $html } If you also collected some useful PHP codes
http://www.bkjia.com/PHPjc/632739.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632739.htmlTechArticleThere are many PHP code snippets on the Internet that can improve development efficiency, and you can also learn the techniques and apply them to your own projects , below I love boiled fish and have selected a few of the more useful ones...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template