新蒲京200.c软件下载-app官网网址 > 问答 >

付费投稿计划

本文由码农网澳门新蒲京官网网址, – 小峰原创翻译,转发请看清文末的转发供给,款待出席大家的付费投稿布署!

在支付网址、app或博客时,代码片段能够真正地为你节省时间。前几天,我们就来分享一下本人搜聚的部分极品有用的PHP代码片段。一齐来看朝气蓬勃看吧!

澳门新蒲京官网网址 1

1.创造数量UENCOREI

数量UTiggoI在松开图像到HTML / CSS / JS中以节约HTTP诉求时十一分有用,况兼能够减小网址的加载时间。上面的函数能够创造基于$file的数量U君越I。

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

2.合并JavaScript和CSS文件

另一个得以尽量减弱HTTP央浼和节省页面加载时间的好提议是:合并你的CSS和JS文件。即便笔者更提议我们利用专项使用插件(例如minify),但利用PHP来合併文件也极度轻松。大家来看一下:

function combine_my_files($array_files, $destination_dir, $dest_file_name){
 if(!is_file($destination_dir . $dest_file_name)){ //continue only if file doesn't exist
 $content = "";
 foreach ($array_files as $file){ //loop through array list
 $content .= file_get_contents($file); //read each file
 }
 //You can use some sort of minifier here
 //minify_my_js($content);
 $new_file = fopen($destination_dir . $dest_file_name, "w" ); //open file for writing
 fwrite($new_file , $content); //write to destination
 fclose($new_file);
 return '<script src="'. $destination_dir . $dest_file_name.'"></script>'; //output combined file
 }else{
 //use stored file
 return '<script src="'. $destination_dir . $dest_file_name.'"></script>'; //output combine file
 }
}

同临时候,用法是如此的:

$files = array(
 'http://example/files/sample_js_file_1.js',
 'http://example/files/sample_js_file_2.js',
 'http://example/files/beautyquote_functions.js',
 'http://example/files/crop.js',
 'http://example/files/jquery.autosize.min.js',
 );
echo combine_my_files($files, 'minified_files/', md5("my_mini_file").".js");

3.翻看你的电子邮件是或不是已读

当发送电子邮件时,你会期待知道你的邮件是还是不是已读。这里有二个可怜有意思的代码片段,它能够记录阅读你邮件的IP地址,甚至实际的日子和时间。

<?
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 . ' ' . "rn";
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);
?>

4.从网页提取关键词

正如那小题目所说的那样:这一个代码片段能让您轻便地从网页中提取元根本词。

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

5.查找页面上的保有链接

运用DOM,你能够轻巧地抓取来网页上的富有链接。这里有二个办事示范:

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

6.自动转变UPRADOL为可点击的超链接

在WordPress中,假让你想在字符串中机动转变全数的U宝马7系L成可点击的超链接,那么使用内置函数make_clickable(卡塔尔国能够令你思量事成。假若您须要在WordPress之外这么做,那么你能够在wp-includes/formatting.php参照他事他说加以考查该函数的源代码:

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] . "<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(substr($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 here was 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 is not in an 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;
}

7.在你的服务器上下载并保留远程图像

在长间隔服务器上下载一个图像,并将其保存在融洽的服务器上,在创设网址时很有用,并且那也相当轻巧产生。上边包车型地铁这两行代码就会为您办到。

$image = file_get_contents('http://www.url.com/image.jpg');
file_put_contents('/images/image.jpg', $image); //Where to save the image

8.检查评定浏览器语言

假如您的网址使用各类语言,那么检查测验浏览器语言,并将这种语言作为暗中同意语言会很有用。上边包车型地铁代码将赶回客商浏览器接纳的言语。

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

9.全文突显Facebook粉丝的多寡

举例你的网址或博客有二个Instagram的页面,那么你可能想要突显你有稍许个观者。那个代码片段能够帮助你获得推特客官的数量。不要遗忘在其次行加多你的页面ID。页面ID能够在地方http://facebook.com/yourpagename/info找到。

<?php
$page_id = "302807633129400";
$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;
?>
下一篇:没有了