为开发者准备的9个实用PHP代码片段(转)
[导读] 当你开发网站、app或博客系统时,如果有一些实用的代码片段可以直接使用,就可以节省你大量的时间和精力。这篇文章就为你分享几个实用的PHP代码片段,帮助你的Web开发。
一.查看邮件是否已被阅读
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);
?>
源代码:http://www.emoticode.net/php/code-to-find-out-if-your-email-has-been-read.html
二.从网页中提取关键词
$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 );
源代码:http://www.emoticode.net/php/extract-keywords-from-any-webpage.html
三.查找页面上的所有链接
$html = file_get_contents('http://www.php100.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.'';
}
源代码:http://snipplr.com/view/70489/find-all-links-on-a-page/
四.自动转换URL为可点击超链接
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 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("#(]+?>|>))]+?>([^>]+?)#i", "$1$3", $ret);
$ret = trim($ret);
return $ret;
}
源代码:http://zenverse.net/php-function-to-auto-convert-url-into-hyperlink/
五.创建数据URI
function data_uri($file, $mime) {
$contents=file_get_contents($file);
$base64=base64_encode($contents);
echo "data:$mime;base64,$base64";
}
源代码:http://css-tricks.com/snippets/php/create-data-uris/
六.下载和保存远程图片到你的服务器
$image = file_get_contents('http://www.php100.com/image.jpg');
file_put_contents('/images/image.jpg', $image); //Where to save the image
源代码:http://www.catswhocode.com/blog/snippets/download-save-a-remote-image-on-your-server-using-php
七.移除Microsoft Word HTML标签
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
}
源代码:http://tim.mackey.ie/CommentView,guid,2ece42de-a334-4fd0-8f94-53c6602d5718.aspx
八.检测浏览器语言
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;
}
源代码:http://snipplr.com/view/12631/detect-browser-language/
九.显示Facebook上的粉丝数量
$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;
?>
源代码:http://www.wprecipes.com/display-number-of-facebook-fans-in-full-text-on-your-wordpress-blog
原文:http://www.catswhocode.com/blog/useful-snippets-for-php-developers
译文:http://www.php100.com/html/dujia/2015/0108/8305.html
(翻译:PHP100_Zeroing)
转载于:https://www.cnblogs.com/huojing/articles/4521840.html
为开发者准备的9个实用PHP代码片段(转)相关推荐
- 分享几个实用的代码片段(附代码例子)
大家好,我是杂烩君. 本次来分享几个实用的代码小片段. 获取CPU温度 应用可以定时获取CPU的温度,比如程序异常崩溃时,我们可能需要分析多方面原因,CPU温度就是其中之一. 代码: 左右滑动查看全部 ...
- 第十一期:30秒内便能学会的30个实用Python代码片段
许多人在数据科学.机器学习.web开发.脚本编写和自动化等领域中都会使用Python,它是一种十分流行的语言. Python流行的部分原因在于简单易学. 本文将简要介绍30个简短的.且能在30秒内掌握 ...
- python sorted下标_Python列表实用的代码片段
Python列表知识点汇总,我总结成了 5 个字:增删改查序. 就是对列表进行新增元素.删除元素.改变元素.查询元素.列表排序. 接下来对应展开为 Python 习题. 增 对列表新增元素,有四种方式 ...
- 【移动开发】Android应用程序中实用的代码框架(一)
好久没有更新博客了,这段时间里和我的小伙伴们("乌索普"."丁二爷"."小娜".'小雯'')参加了一个大学生Android应用软件比赛,利用 ...
- Web开发者的福利 30段超实用CSS代码
上周,研发频道发表了一篇" Web开发者不容错过的20段CSS代码",大家一致觉得很实用.该文是笔者对后30个的翻译,希望对大家有帮助. 1.花式连字符(&) 这个类应该在 ...
- mac下实用的代码编辑器
如今网络上有很多针对 Mac 平台的代码编辑器,因此要找到正确的.最适合你的代码编辑器可能需要一段时间.这里主要整理了 13 款实用的代码编辑器,一些是免费.开源的,也有一些收费的,但你可以免费试用. ...
- Web开发者的福利 30段超实用CSS代码笔记
1.花式连字符(&) 这个类应该在span元素里使用,并且里面包括&字符.它使用经典的serif字体和斜体来增强&符号. 1 2 3 4 5 .amp { font-f ...
- 即学即用的 30 段 Python 非常实用的代码
点击上方"视学算法",马上关注 真爱,请置顶或星标 图片来自 Jantine Doornbos on Unsplash 原标题 | 30 Helpful Python Snippe ...
- ajax 示例代码,Ajax的简单实用实例代码
我将实现一个简单的Ajax页面无刷新进行用户验证案例: 效果如下图: 实现主要过程: 在UsersAction类中的checkUser方法中接收并验证前台的表单数据,针对不同情况,返回一个状态码cod ...
最新文章
- 计算机网络工程实用技术考试,计算机网络实用技术期中考试复习题.doc
- 合伙人分开的一点思考
- 47 jQuery文本内容值
- 每日一皮:即使是个小BUG,有时候也能吓你半条命!
- 百度今晨7:20分“宕机”
- python中构造方法可以被继承吗_python – 类继承:构造函数应该兼容吗?多重继承的情况?...
- latex 小于_一份菜鸡的Latex课堂作业works--(ii)
- .NET 程序测试 Java 项目 log4j2 是否存在远程代码执行漏洞
- (译)你应该知道的jQuery技巧
- [内核同步]自旋锁spin_lock、spin_lock_irq 和 spin_lock_irqsave 分析
- testng执行参数_初识TestNG测试框架
- Android Support Library更新到v22.1之AppCompat新特性
- 【渝粤教育】国家开放大学2018年秋季 7048-21T危急重症护理学(本) 参考试题
- 台达A2/B2伺服电机编码器改功率软件 台达A2/B2伺服电机编码修改, 用于更换编码器写匹配电机参数
- Unable to run mksdcard SDK tool
- 一款好看+极简到不行的HTML5音乐播放器-skPlayer
- STM32网络通信之DM9000了解
- 学海无涯!最全Android面试知识点梳理,系列篇
- 突然有一天,我老无所依
- RadioButton 和 RadioButtonList 比较
热门文章
- treegrid 如何获取getchanges inserted_如何避开Vue性能优化之路的荆棘?
- curl在android服务器上编译,Android curl的上载编译和使用
- php 判断同时存在英文跟数字,php判断输入是否是纯数字,英文,汉字的方法
- 12306能删候补订单记录_「实用」官方“捡漏”神器来了!12306铁路候补购票服务正式上线!...
- linux shell 变量减法_Linux Shell (3) - 变量运算
- 有机晶体数据库_Cambridge Structural Database 2017 晶体结构分析软件分享
- stm32 I2C架构
- 使用权值衰减算法解决神经网络过拟合问题、python实现
- Java 作用域修饰符
- 大学计算机基础知识判断题,大学计算机基础学习知识判断题.doc