10个超级有用的PHP代码片段果断收藏


Posted in PHP onSeptember 23, 2015

本文小编将为你奉上10个超级有用的PHP代码片段。

10个超级有用的PHP代码片段果断收藏

1.查找Longitudes与Latitudes之间的距离

function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) { 
 $theta = $longitude1 - $longitude2; 
 $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta))); 
 $miles = acos($miles); 
 $miles = rad2deg($miles); 
 $miles = $miles * 60 * 1.1515; 
 $feet = $miles * 5280; 
 $yards = $feet / 3; 
 $kilometers = $miles * 1.609344; 
 $meters = $kilometers * 1000; 
 return compact('miles','feet','yards','kilometers','meters'); 
} 
 
$point1 = array('lat' => 40.770623, 'long' => -73.964367); 
$point2 = array('lat' => 40.758224, 'long' => -73.917404); 
$distance = getDistanceBetweenPointsNew($point1['lat'], $point1['long'], $point2['lat'], $point2['long']); 
foreach ($distance as $unit => $value) { 
 echo $unit.': '.number_format($value,4).' 
'; 
} 
 
The example returns the following: 
 
miles: 2.6025 
feet: 13,741.4350 
yards: 4,580.4783 
kilometers: 4.1884 
meters: 4,188.3894

2.完善cURL功能

 

function xcurl($url,$ref=null,$post=array(),$ua="Mozilla/5.0 (X11; Linux x86_64; rv:2.2a1pre) Gecko/20110324 Firefox/4.2a1pre",$print=false) { 
 $ch = curl_init(); 
 curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
 if(!empty($ref)) { 
 curl_setopt($ch, CURLOPT_REFERER, $ref); 
 } 
 curl_setopt($ch, CURLOPT_URL, $url); 
 curl_setopt($ch, CURLOPT_HEADER, 0); 
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
 if(!empty($ua)) { 
 curl_setopt($ch, CURLOPT_USERAGENT, $ua); 
 } 
 if(count($post) > 0){ 
 curl_setopt($ch, CURLOPT_POST, 1); 
 curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
 } 
 $output = curl_exec($ch); 
 curl_close($ch); 
 if($print) { 
 print($output); 
 } else { 
 return $output; 
 } 
}

3.清理用户输入

]*?>.*?@si', // Strip out javascript 
 '@<[\/\!]*?[^<>]*?>@si',  // Strip out HTML tags 
 '@]*?>.*?@siU', // Strip style tags properly 
 '@@'  // Strip multi-line comments 
 ); 
 
 $output = preg_replace($search, '', $input); 
 return $output; 
 } 
?> 
$val) { 
  $output[$var] = sanitize($val); 
 } 
 } 
 else { 
 if (get_magic_quotes_gpc()) { 
  $input = stripslashes($input); 
 } 
 $input = cleanInput($input); 
 $output = mysql_real_escape_string($input); 
 } 
 return $output; 
} 
?>

4.通过IP(城市、国家)检测地理位置

function detect_city($ip) { 
 
 $default = 'Hollywood, CA'; 
 
 if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost')  $ip = '8.8.8.8';  $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';  $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip);  $ch = curl_init();  $curl_opt = array(  CURLOPT_FOLLOWLOCATION => 1, 
  CURLOPT_HEADER => 0, 
  CURLOPT_RETURNTRANSFER => 1, 
  CURLOPT_USERAGENT => $curlopt_useragent, 
  CURLOPT_URL => $url, 
  CURLOPT_TIMEOUT  => 1, 
  CURLOPT_REFERER  => 'http://' . $_SERVER['HTTP_HOST'], 
 ); 
 
 curl_setopt_array($ch, $curl_opt); 
 
 $content = curl_exec($ch); 
 
 if (!is_null($curl_info)) { 
  $curl_info = curl_getinfo($ch); 
 } 
 
 curl_close($ch); 
 
 if ( preg_match('{ 
 
 
City : ([^<]*) 
}i', $content, $regs) ) { $city = $regs[1]; } if ( preg_match('{ 
 
State/Province : ([^<]*) 
 
}i', $content, $regs) ) { $state = $regs[1]; } if( $city!='' && $state!='' ){ $location = $city . ', ' . $state; return $location; }else{ return $default; } }

5.设置密码强度

100){ 
 $strength = 100; 
 } 
 return $strength; 
} 
 
var_dump(password_strength("Correct Horse Battery Staple")); 
echo " 
"; 
var_dump(password_strength("Super Monkey Ball")); 
echo " 
"; 
var_dump(password_strength("Tr0ub4dor&3")); 
echo " 
"; 
var_dump(password_strength("abc123")); 
echo " 
"; 
var_dump(password_strength("sweet"));

6.检测浏览器语言,只提供可用的$availableLanguages作为数组(‘en', ‘de', ‘es')

function get_client_language($availableLanguages, $default='en'){ 
 
 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { 
  
 $langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']); 
 
 //start going through each one 
 foreach ($langs as $value){ 
 
  $choice=substr($value,0,2); 
  if(in_array($choice, $availableLanguages)){ 
  return $choice; 
   
  } 
  
 } 
 } 
 return $default; 
}

7.创建数据URL

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

8.创建更加友好的页面标题SEO URL

输入示例:$title = “This foo's bar is rockin' cool!”; echo makeseoname($title); //RETURNS: //this-foos-bar-is-rockin-cool

function make_seo_name($title) { 
 return preg_replace('/[^a-z0-9_-]/i', '', strtolower(str_replace(' ', '-', trim($title)))); 
}

9.终极加密功能

// f(ucking) u(ncrackable) e(ncryption) function by BlackHatDBL (www.netforme.net) 
function fue($hash,$times) { 
 // Execute the encryption(s) as many times as the user wants 
 for($i=$times;$i>0;$i--) { 
 // Encode with base64... 
 $hash=base64_encode($hash); 
 // and md5... 
 $hash=md5($hash); 
 // sha1... 
 $hash=sha1($hash); 
 // sha256... (one more) 
 $hash=hash("sha256", $hash); 
 // sha512 
 $hash=hash("sha512", $hash); 
 
 } 
 // Finaly, when done, return the value 
 return $hash; 
}

10a.Tweeter Feed Runner——使用任意twitter名,可在任意页面上加载用户资源。

pversion; 
 } 
 public function loadTimeline($user, $max = 20){ 
 $this->twitURL .= 'statuses/user_timeline.xml?screen_name='.$user.'&count='.$max; 
 $ch = curl_init(); 
 curl_setopt($ch, CURLOPT_URL, $this->twitURL); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
 $this->xml = curl_exec($ch); 
 return $this; 
 } 
 public function getTweets(){ 
 $this->twitterArr = $this->getTimelineArray(); 
 $tweets = array(); 
 foreach($this->twitterArr->status as $status){ 
  $tweets[$status->created_at->__toString()] = $status->text->__toString(); 
 } 
 return $tweets; 
 } 
 public function getTimelineArray(){ 
 return simplexml_load_string($this->xml); 
 } 
 public function formatTweet($tweet){ 
 $tweet = preg_replace("/(http(.+?))( |$)/","$1$3", $tweet); 
 $tweet = preg_replace("/#(.+?)(\h|\W|$)/", "#$1$2", $tweet); 
 $tweet = preg_replace("/@(.+?)(\h|\W|$)/", "@$1$2", $tweet); 
 return $tweet; 
 } 
}

10b. Tweeter Feed Runner——用于在主题中创建文件,比如:example.php

loadTimeline("phpsnips")->getTweets(); 
foreach($feed as $time => $message){ 
 echo "<div class='tweet'>".$twitter->formatTweet($message)."<br />At: ".$time."</div>"; 
}

直接拿来用,10个PHP代码片段,还犹豫什么,果断收藏吧

PHP 相关文章推荐
php 小乘法表实现代码
Jul 16 PHP
PHP Reflection API详解
May 12 PHP
如何使用Gitblog和Markdown建自己的博客
Jul 31 PHP
深入分析PHP优化及注意事项
Jul 04 PHP
Symfony2获取web目录绝对路径、相对路径、网址的方法
Nov 14 PHP
关于PHP内置的字符串处理函数详解
Feb 04 PHP
php实现网页端验证码功能
Jul 11 PHP
yii2多图上传组件的使用教程
May 10 PHP
Laravel5.7 Eloquent ORM快速入门详解
Apr 12 PHP
PHP使用Redis实现Session共享的实现示例
May 12 PHP
PHP实现的抓取小说网站内容功能示例
Jun 27 PHP
PHP大文件切割上传功能实例分析
Jul 01 PHP
Discuz!X中SESSION机制实例详解
Sep 23 #PHP
php中session_id()函数详细介绍,会话id生成过程及session id长度
Sep 23 #PHP
通过修改配置真正解决php文件上传大小限制问题(nginx+php)
Sep 23 #PHP
php实现简单的MVC框架实例
Sep 23 #PHP
php实现的单一入口应用程序实例分析
Sep 23 #PHP
PHP中常见的缓存技术实例分析
Sep 23 #PHP
PHPStrom中实用的功能和快捷键大全
Sep 23 #PHP
You might like
PHP的Yii框架中移除组件所绑定的行为的方法
2016/03/18 PHP
js不是基础的基础
2006/12/24 Javascript
将函数的实际参数转换成数组的方法
2010/01/25 Javascript
文本框input聚焦失焦样式实现代码
2012/10/12 Javascript
js实现网站首页图片滚动显示
2013/02/04 Javascript
jquery+json实现数据列表分页示例代码
2013/11/15 Javascript
JavaScript实现找出字符串中第一个不重复的字符
2014/09/03 Javascript
AngularJS语法详解
2015/01/23 Javascript
浅谈js中的in-for循环
2016/06/28 Javascript
Angular设置title信息解决SEO方面存在问题
2016/08/19 Javascript
js实现文字向上轮播功能
2017/01/13 Javascript
基于Bootstrap模态对话框只加载一次 remote 数据的解决方法
2017/07/09 Javascript
ES6下子组件调用父组件的方法(推荐)
2018/02/23 Javascript
JavaScript封装的常用工具类库bee.js用法详解【经典类库】
2018/09/03 Javascript
vue2.0 路由模式mode=&quot;history&quot;的作用
2018/10/18 Javascript
vue router的基本使用和配置教程
2018/11/05 Javascript
使用Element的InfiniteScroll 无限滚动组件报错的解决
2020/07/27 Javascript
JavaScript 防盗链的原理以及破解方法
2020/12/29 Javascript
[01:31]DOTA2上海特级锦标赛 SECRET战队完整宣传片
2016/03/16 DOTA
python模拟登陆阿里妈妈生成商品推广链接
2014/04/03 Python
Python的Scrapy爬虫框架简单学习笔记
2016/01/20 Python
python并发编程之线程实例解析
2017/12/27 Python
Python的多维空数组赋值方法
2018/04/13 Python
完美解决Python 2.7不能正常使用pip install的问题
2018/06/12 Python
解决Tensorflow使用pip安装后没有model目录的问题
2018/06/13 Python
对pandas将dataframe中某列按照条件赋值的实例讲解
2018/11/29 Python
Python Django基础二之URL路由系统
2019/07/18 Python
Python 处理文件的几种方式
2019/08/23 Python
Python基于DB-API操作MySQL数据库过程解析
2020/04/23 Python
OSPREY LONDON官网:英国本土皮具品牌
2019/05/31 全球购物
临床医学专业毕业生的自我评价
2013/10/17 职场文书
就业自荐信
2013/12/04 职场文书
机械电子工程专业自荐书
2014/06/10 职场文书
详解GaussDB for MySQL性能优化
2021/05/18 MySQL
MySQL索引 高效获取数据的数据结构
2022/05/02 MySQL
python中validators库的使用方法详解
2022/09/23 Python