PHP 常用函数库和一些实用小技巧
PHP 常用函数库和一些实用小技巧
//文件读取函式
function PHP_Read($file_name) {
$fd=fopen($file_name,r);
while($bufline=fgets($fd, 4096)){
$buf.=$bufline;
}
fclose($fd);
return $buf;
}
?>
文件写入函式
//文件写入函式
function PHP_Write($file_name,$data,$method="w") {
$filenum=@fopen($file_name,$method);
flock($filenum,LOCK_EX);
$file_data=fwrite($filenum,$data);
fclose($filenum);
return $file_data;
}
?>
静态页面生成函式
//静态页面生成函式
function phptohtm($filefrom,$fileto,$u2u=1){
if($u2u==1){
$data=PHP_Read($filefrom);
}else{
$data=$filefrom;
}
PHP_Write($fileto,$data);
return true;
}
?>
指定条件信息数量检索函式
//指定条件信息数量检索函式
function rec_exist($table,$where){
$query="select count(*) as num from $table ".$where;
$result=mysql_query($query) or die(nerror(1));
$rowcount=mysql_fetch_array($result);
$num=$rowcount["num"];
if ($num==0){
return false;
}
return $num;
}
?>
目录删除函式
//目录删除函式
function del_DIR($directory){
$mydir=dir($directory);
while($file=$mydir->read()){
if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!="..")){
del_DIR("$directory/$file");
}else{
if(($file!=".") AND ($file!="..")){
unlink("$directory/$file");
//echo "unlink $directory/$file ok ";
}
}
}
$mydir->close();
rmdir($directory);
//echo "rmdir $directory ok ";
}
?>
目录名称合法性检测函式
//目录名称合法性检测
function isen($str){
$ret="";
for($i=0;$i $p=ord(substr($str,$i,1));
if(($p<48 & $p!=45 & $p!=46) || ($p>57 & $p<65) || ($p>90 & $p<97 & $p!=95) || $p>122){
nerror("不符合规范!");
}
$ret.=chr($p);
}
return $ret;
}
?>
分页函式
//分页函式
function splitlist($HALT,$LRLIST,$ECHOCNT,$paper,$table,$where,$page_id,$userid){
global $splitstr,$sumcnt;
if($paper=="" || $sumcnt==""){
$query = "select count(*) as num from $table $where";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$sumcnt=$row["num"];
if($sumcnt==0){
nerror("该版内还没有选择发布新闻 !");
}
$paper=1;
}
$sumpaper=($sumcnt-$sumcnt%$ECHOCNT)/$ECHOCNT;
if(($sumcnt%$ECHOCNT)!=0) $sumpaper+=1;
if($sumpaper==1 && $HALT==0) return($where);
$enwhere=base64_encode(base64_encode($where));
if(($LRLIST*2+1) < $sumpaper){
if(($paper-$LRLIST) < 2){
$tract=1;
$sub=$LRLIST*2+1;
}else if(($paper+$LRLIST) >= $sumpaper){
$tract=$sumpaper-($LRLIST*2);
$sub=$sumpaper;
}else{
$tract=$paper-$LRLIST;
$sub=$paper+$LRLIST;
}
}else{
$tract=1;
$sub=$sumpaper;
}
$uppaper=$paper-1;
$downpaper=$paper+1;
$startcnt=($paper-1)*$ECHOCNT;
$where.=" limit ${ startcnt },${ ECHOCNT }";
if($tract > 1) { $splitstr="【 << "; }
else $splitstr="【 << ";
for($i=$tract;$i<=$sub;$i++){
if ($i!=$paper) $splitstr.="".$i." ";
else $splitstr.="".$i." ";
}
if ($sub!=$sumpaper) $splitstr.=">> 】";
else $splitstr.=">> 】";
return($where);
}
?>
关于分页函式的使用说明
/*
#### 检索分页函式 ####
Int $HALT - 检索结果仅分1页时是否(1/0)显示页码条
Int $LRLIST - (页码条显示页码数-1)/2
Int $ECHOCNT - 检索时每页显示记录的数量
Int $paper - 页数,预提取:$paper=$HTTP_GET_VARS[paper];
Varchar $table - 数据表名,预附值:$table="db.table";
Varchar $where - 检索条件,预附值:$where="where field='value'";
Varchar $enwhere - 将原$where进行两次base64_encode()编码后以GET的方式提交
Varchar $splitstr - 页码条输出字串,执行函式后在相应的位置执行 echo $splitstr;
函式调用前需获取变量 -
$paper=$HTTP_GET_VARS[paper];
$sumcnt=$HTTP_GET_VARS[sumcnt];
$enwhere=$HTTP_GET_VARS[enwhere];
Return (Varchar $where) - 分页后检索语句的检索条件
注意:本函式需调用出错处理函式 nerror($error);
*/
图片文件上传函式
//图片文件上传函式
function upload_img($UploadFile,$UploadFile_name,$UploadFile_size,$UploadPath,$max_size=64){
//$TimeLimit=60; //设置超时限制时间 缺省时间为 30秒 设置为0时为不限时
//set_time_limit($TimeLimit);
if(($UploadFile!= "none" )&&($UploadFile != "" )){
$FileName=$UploadPath.$UploadFile_name;
if($UploadFile_size <1024){
$FileSize="(string)$UploadFile_size" . "字节";
}elseif($UploadFile_size <(1024 * $max_size)){
$FileSize=number_format((double)($UploadFile_size / 1024), 1) . " KB";
}else{
nerror("文件超过限制大小!");
}
//{
//$FileSize="number_format((double)($UploadFile_size" / (1024 * 1024)), 1) . " MB";
// }
if(!file_exists($FileName)){
if(copy($UploadFile,$FileName)){
return "$UploadFile_name ($FileSize)";
}else{
nerror("文件 $UploadFile_name 上载失败!");
}
unlink($UploadFile);
}else{
nerror("文件 $UploadFile_name 已经存在!");
}
//set_time_limit(30); //恢复缺省超时设置
}
}
以下是一些小技巧:
如何判断ip地址合法性
if(!strcmp(long2ip(sprintf("%u",ip2long($ip))),$ip)) echo "is ipn";
----
email的正则判断
eregi("^[_.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z_-]+.)+[a-zA-Z]$", $email);
检测ip地址和mask是否合法的例子
$ip = '192.168.0.84';
$mask = '255.255.255.0';
$network = '192.168.0';
$ip = ip2long($ip);
$mask = ip2long($mask);
$network = ip2long($network);
if( ($ip & $mask) == $network) echo "valid ip and maskn";
?>
----
文件下载头部输出如何设定
header("Content-type: application/x-download");
header("Content-Disposition: attachment; filename=$file_download_name;");
header("Accept-Ranges: bytes");
header("Content-Length: $download_size");
echo 'xxx'
用header输出ftp下载方式,并且支持断点续传
一个例子:
header('Pragma: public');
header('Cache-Control: private');
header('Cache-Control: no-cache, must-revalidate');
header('Accept-Ranges: bytes');
header('Connection: close');
header("Content-Type: audio/mpeg");
header("Location:ftp://download:1bk3l4s3k9s2@232.2.22.22/2222/web技术开发知识库/cn_web.rmvb");
正则匹配中文
ereg("^[".chr(0xa1)."-".chr(0xff)."]+$", $str);
批量替换文本里面的超级链接
function urlParse($str = ''){
if ('' == $str) return $str;
$types = array("http", "ftp", "https");
$replace = <<
''.htmlentities('\1').htmlentities('\2').''
EOPHP;
$ret = $str;
while(list(,$type) = each($types)){
$ret = preg_replace("|($type://)([^\s]*)|ie ", $replace, $ret);
}
return $ret;
}
PHP 常用函数库和一些实用小技巧的更多相关文章
- 【MySQL基础总结】常用函数库
常用函数库 数学函数 分类及含义 示例 字符串函数 分类及含义 示例 日期时间函数 分类及含义 示例 条件判断函数 分类及含义 示例 系统函数 分类及含义 加密函数 分类及定义 其他常用函数 分类及含 ...
- VC6.0实用小技巧
VC6.0的若干实用小技巧 .检测程序中的括号是否匹配 把光标移动到需要检测的括号(如大括号{}.方括号[].圆括号()和尖括号<>)前面,键入快捷键 “Ctrl+]”.如果括号匹配正确, ...
- Vim实用小技巧
Vim实用小技巧 一些网络上质量较高的Vim资料 从我07年接触Vim以来,已经过去了8个年头,期间看过很多的Vim文章,我自己觉得非常不错,而且创作时间也比较近的文章有如下这些. Vim入门 目前为 ...
- 实用小技巧(一):UIScrollView中上下左右滚动方向的判断
https://www.jianshu.com/p/93e8459b6dae 2017.06.01 01:13* 字数 674 阅读 1201评论 0喜欢 1 2017.06.01 01:13* 字数 ...
- svn checkout 实用小技巧
svn checkout 实用小技巧 by:授客 QQ:1033553122 问题描述: 用svn小乌龟软件,进行update,commit之前,先要把svn工作目录checkout到本地,那么问 ...
- 必看!macOS进阶不得不知的实用小技巧
不知道大家对使用苹果电脑的体验如何?您充分利用您的mac了吗?其实macOS上存在着许多快捷方式和技巧可以帮助简化我们的工作流程,提高效率,但是在日常生活中经常被人们忽略或者遗忘.以下是macdown ...
- 实用小技巧:Notepad++直接连接Linux
实用小技巧:Notepad++直接连接Linux 前言 号称编辑器之神的Vim对于只会用几个基础操作的本人而言,在编辑一些大型文本有那么些力不从心: 平时都是通过Xftp拖到本地,修改完后再覆盖回去: ...
- JSFunction-Javascript常用函数库
最近正在整理书写常用的Javascript函数库,此函数库近期会持续更新 JSFunction 这里可以找到你经常想要使用的js函数,我正在努力完善它 希望它对你有所帮助 相信代码是优雅的舞者.--北 ...
- c++函数库中一些实用的函数
有一些程序,虽然写起来不难,但是可能比较麻烦或容易出错,这时就可以用c++函数库里自带的一些实用的函数. 这里只记录一些不太常见的函数. ------------------------------- ...
随机推荐
- oracle 10g 学习之触发器(13)
真实使用场景:数据备份 1. 触发器的 helloworld: 编写一个触发器, 在向 emp 表中插入记录时, 打印 'helloworld' create or replace trigger e ...
- jquery easy ui 1.3.4 按钮(button)(6)
6.1.linkbutton linkbutton是将一个<a>标签包装成一个能显示图片.文字.的超链接按钮 如何给linkbutton添加一个事件? 使用JQ的方式就能给linkbutt ...
- Codeforces Round #Pi (Div. 2) A. Lineland Mail 水
A. Lineland MailTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567/proble ...
- uva 10972(边双连通分量)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33804. 思路:和poj的一道题有点像,不过这道题图可能不连通,因 ...
- Failed to load or instantiate
Failed to load or instantiate: add this code in your xml: xmlns:android="http://schemas.android ...
- http://jingyan.baidu.com/album/d8072ac47baf0eec95cefdca.html?picindex=4
http://jingyan.baidu.com/album/d8072ac47baf0eec95cefdca.html?picindex=4
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- 1^b+2^b+3^b+...+n^b数列
首先,这是我自己推出来的,O(n^2),常数巨大.所以无能为力优化!所以求此数列的公式!求优化!!! 主要思想:要算b次的,那么就要先算b+1次的. 首先,我用F(i, j)表示杨辉三角第i层第j个, ...
- HDU 4679 Terrorist’s destroy
如果不在最长路的边,那么肯定是w*最长路. 如果在最长路,那么把最长路分成两段,左边树的最长路就是左段+左边点的次短路(不包含最长路上的点的最长路) ,右边同理. 还有就是更新,经过左端点的最长路,不 ...
- HDNOIP201404最短路径
HDNOIP201404最短路径 难度级别: A: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 a.b.c是3个互不相等的1 ...