15.在字符断点处截断文字
所谓断字(wordbreak)
,即一个单词可在转行时断开的地方
。这一函数将在断字处截断字符串
。 //Original
PHPcodebyChirpInternet:www.chirp.com.au
//Pleaseacknowledgeu
seofthiscodebyincludingthisheader.
functionmyTruncate($string,$limit,$break=".",$pad="..."){
//returnwithnochangeifstringisshorterthan$limit
if(strlen($string)<=$limit)
return$string;
//is$breakpresentbetween$limitandtheendofthestring?
if(false!==($breakpoint=strpos($string,$break,$limit))){
if($breakpoint<strlen($string)-1){
$string=substr($string,0,$breakpoint).$pad;
}
}
return$string;
}
/*****Example****/
$short_string=myTruncate($long_string,100,'');
16.文件Zip压缩
/*createsacompressedzipfile*/
functioncreate_zip($files=array(),$destination='',$overwrite=false){
//ifthezipfilealreadyexistsandoverwriteisfalse,returnfalse
if(file_exists($destination)&&!$overwrite){returnfalse;}
//vars
$valid_files=array();
//iffileswerepassedin...
if(is_array($files)){
//cyclethrougheachfile
foreach($filesas$file){
//makesurethefileexists
if(file_exists($file)){
$valid_files[]=$file;
}
}
}
//ifwehavegoodfiles...
if(count($valid_files)){
//createthearchive
$zip=newZipArchive();
if($zip->open($destination,$overwrite?ZIPARCHIVE::OVERWRITE:ZIPARCHIVE::CREATE)!==true){
returnfalse;
}
//addthefiles
foreach($valid_filesas$file){
$zip->addFile($file,$file);
}
//debug
//echo'Theziparchivecontains',$zip->numFiles,'fileswithastatusof',$zip->status;
//closethezip--done!
$zip->close();
//checktomakesurethefileexists
returnfile_exists($destination);
}
else
{
returnfalse;
}
}
/*****ExampleUsage***/
$files=array('file1.jpg','file2.jpg','file3.gif');
create_zip($files,'myzipfile.zip',true);
17.解压缩Zip文件
/**********************
*@file-pathtozipfile *@destination-destinationdirectoryforunzippedfiles */
functionunzip_file($file,$destination){
//createobject
$zip=newZipArchive();
//openarchive
if($zip->open($file)!==TRUE){
die(’Couldnotopenarchive’);
}
//extractcontentstodestinationdirectory
$zip->extractTo($destination);
//closearchive
$zip->close();
echo'Archiveextractedtodirectory';
}
18.为URL地址预设http字符串
有时需要接受一些表单中的网址输入
,但用户很少添加http://字段,此代码将为网址添加该字段。
if(!preg_match("/^(http|ftp):/",$_POST['url'])){
$_POST['url']='http://'.$_POST['url'];
}
19.将网址字符串转换成超级链接
该函数将URL和E-mail地址字符串转换为可点击的超级链接。
functionmakeClickableLinks($text){
$text=eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)',
'<ahref="1">1</a>',$text);
$text=eregi_replace('([[:space:]()[{}])(
www.[-a-zA-Z0-9@:%_+.~#?&//=]+)',
'1<ahref="
http://2">2</a>',$text);
$text=eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})',
'<ahref="
mailto:1">1</a>',$text);
return$text;
}
20.调整图像尺寸
创建图像缩略图需要许多时间,此代码将有助于了解缩略图的逻辑。
/**********************
*@filename-pathtotheimage *@tmpname-temporarypathtothumbnail *@xmax-maxwidth *@ymax-maxheight */
functionresize_image($filename,$tmpname,$xmax,$ymax)
{
$ext=explode(".",$filename);
$ext=$ext[count($ext)-1];
if($ext=="jpg"||$ext=="jpeg")
$im=imagecreatefromjpeg($tmpname);
elseif($ext=="png")
$im=imagecreatefrompng($tmpname);
elseif($ext=="gif")
$im=imagecreatefromgif($tmpname);
$x=imagesx($im);
$y=imagesy($im);
if($x<=$xmax&&$y<=$ymax)
return$im;
if($x>=$y){
$newx=$xmax;
$newy=$newx*$y/$x;
}
else{
$newy=$ymax;
$newx=$x/$y*$newy;
}
$im2=imagecreatetruecolor($newx,$newy);
imagecopyresized($im2,$im,0,0,0,0,floor($newx),floor($newy),$x,$y);
return$im2;
}
21.检测Ajax请求
大多数的JavaScript框架如jQuery,Mootools等,在发出Ajax请求时,都会发送额外的HTTP_X_REQUESTED_WITH头部信息,头当他们一个ajax请求,因此你可以在
服务器端侦测到Ajax请求。
if(!emptyempty($_SERVER['HTTP_X_REQUESTED_WITH'])&&strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])=='xmlhttprequest'){
//IfAJAXRequestThen
}else{
//somethingelse
}