欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

苏州人网站 有好多有用的函数 织梦等也有好多有用的函数

shiping1 的头像
例如
判断 utf-8 gbk等

下面是苏州人的部分函数
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package CodeIgniter
 * @author ExpressionEngine Dev Team
 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @since Version 2.0
 * @filesource
 */
 
// ------------------------------------------------------------------------
 
/**
 * Utf8 Class
 *
 * Provides support for UTF-8 environments
 *
 * @package CodeIgniter
 * @subpackage Libraries
 * @category UTF-8
 * @author ExpressionEngine Dev Team
 */
class CI_Utf8 {
 
/**
* Constructor
*
* Determines if UTF-8 support is to be enabled
*
*/
function __construct()
{
log_message('debug', "Utf8 Class Initialized");
 
global $CFG;
 
if (
preg_match('/./u', 'é') === 1 // PCRE must support UTF-8
AND function_exists('iconv') // iconv must be installed
AND ini_get('mbstring.func_overload') != 1 // Multibyte string function overloading cannot be enabled
AND $CFG->item('charset') == 'UTF-8' // Application charset must be UTF-8
)
{
log_message('debug', "UTF-8 Support Enabled");
 
define('UTF8_ENABLED', TRUE);
 
// set internal encoding for multibyte string functions if necessary
// and set a flag so we don't have to repeatedly use extension_loaded()
// or function_exists()
if (extension_loaded('mbstring'))
{
define('MB_ENABLED', TRUE);
mb_internal_encoding('UTF-8');
}
else
{
define('MB_ENABLED', FALSE);
}
}
else
{
log_message('debug', "UTF-8 Support Disabled");
define('UTF8_ENABLED', FALSE);
}
}
 
// --------------------------------------------------------------------
 
/**
* Clean UTF-8 strings
*
* Ensures strings are UTF-8
*
* @access public
* @param string
* @return string
*/
function clean_string($str)
{
if ($this->_is_ascii($str) === FALSE)
{
$str = @iconv('UTF-8', 'UTF-8//IGNORE', $str);
}
 
return $str;
}
 
// --------------------------------------------------------------------
 
/**
* Remove ASCII control characters
*
* Removes all ASCII control characters except horizontal tabs,
* line feeds, and carriage returns, as all others can cause
* problems in XML
*
* @access public
* @param string
* @return string
*/
function safe_ascii_for_xml($str)
{
return remove_invisible_characters($str, FALSE);
}
 
// --------------------------------------------------------------------
 
/**
* Convert to UTF-8
*
* Attempts to convert a string to UTF-8
*
* @access public
* @param string
* @param string - input encoding
* @return string
*/
function convert_to_utf8($str, $encoding)
{
if (function_exists('iconv'))
{
$str = @iconv($encoding, 'UTF-8', $str);
}
elseif (function_exists('mb_convert_encoding'))
{
$str = @mb_convert_encoding($str, 'UTF-8', $encoding);
}
else
{
return FALSE;
}
 
return $str;
}
 
// --------------------------------------------------------------------
 
/**
* Is ASCII?
*
* Tests if a string is standard 7-bit ASCII or not
*
* @access public
* @param string
* @return bool
*/
function _is_ascii($str)
{
return (preg_match('/[^\x00-\x7F]/S', $str) == 0);
}
 
// --------------------------------------------------------------------
 
}
// End Utf8 Class
 
/* End of file Utf8.php */
/* Location: ./system/core/Utf8.php */



<?php
 
function ajxerr($msg)
{
ajxmsg($msg,1);
die;
}
 
function ajxok($msg)
{
ajxmsg($msg,0);
die;
}
 
function ajxmsg($msg,$errno)
{
global $ajx,$flashId;
$u8msg = mb_convert_encoding( $msg , "UTF-8"  , "GBk" );
header("Content-Type: text/html;charset=UTF-8");
echo "$errno|||$u8msg";
 
}
function safe_get($varname , $vartype='str'){   return safe_core( $_GET , $varname , $vartype );}
function safe_post($varname , $vartype='str'){    return safe_core( $_POST , $varname , $vartype );}
function safe_cookie($varname , $vartype='str'){    return safe_core( $_COOKIE , $varname , $vartype );}
function safe_var($val){ $val = str_replace("\\","\\\\",$val); $val = str_replace("'","\\'",$val); return $val; }
//安全变量
function safe_core( $arr , $varname , $vartype )
{
filterArr($arr[$varname]);
 
if (get_magic_quotes_gpc())return $arr[$varname];
if($vartype=='int')return intval($arr[$varname]);
elseif($vartype=='str'||$vartype=='string'){
$val = $arr[$varname];
$val = str_replace("\\","\\\\",$val);
$val = str_replace("'","\\'",$val);
}
return $val;
}
//安全数组 (默认为其里面的元素为string)
function safe_array($arr)
{
filterArr($arr);
 
if (get_magic_quotes_gpc()){
return $arr;
}
foreach ($arr as $key => $val)
{
$val = str_replace("\\","\\\\",$val);
$val = str_replace("'","\\'",$val);
$arr[$key] = $val;
}
return $arr;
}
 
function filterArr($parameter){
if(is_array($parameter)){
foreach($parameter as $para){
filterArr($para);
}
}else if(preg_match("#\b(select|union|update|insert|or|delete)\b#i",$parameter)){
@header("content-type:text/html;charset=bgk");
exit('涉及非法关键字!');
}
}
 
function strLength($str,$charset='utf-8'){
   if($charset=='utf-8') $str = iconv('utf-8','gb2312',$str);
   $num = strlen($str);
   $cnNum = 0;
   for($i=0;$i<$num;$i++){
  if(ord(substr($str,$i+1,1))>127){
  $cnNum++;
  $i++;
 }
   }
   $enNum = $num-($cnNum*2);
   $number = ($enNum/2)+$cnNum;
   return ceil($number);
 }
 
 
function CutString($_String, $_Length, $_Start=0, $_Encode='GBK')
{
$v = 0;
if($_Encode == 'UTF-8')
{
$_Pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
preg_match_all($_Pa, $_String, $_Rarray);
$_SLength = count($_Rarray[0]);
if($_SLength < $_Length) return $_String;
for($i=$_Start; $i<$_SLength; $i++)
{
if($v >= $_Length * 2 - 1) return $_RS.'';
if(ord($_Rarray[0][$i]) > 129) $v += 2;
else $v++;
$_RS .= $_Rarray[0][$i];
}
} else {
$_Start = $_Start * 2;
$_Length = $_Length * 2;
$_Len = strlen($_String);
if($_Len < $_Length) return $_String;
$_Rstring= '';
for($i=$_Start; $i<$_Len; $i++)
{
if($v >= $_Length - 1) return $_Rstring.'';
if(ord(substr($_String, $i, 1)) > 129) { $_Rstring .= substr($_String, $i, 2); $v += 2; $i++; }
else { $_Rstring .= substr($_String, $i, 1); $v++; }
}
return $_Rstring;
}
}
 
function filterGet()
{
foreach( $_GET as $key=>$v )
{
if(!is_array($v))
{
$v =  str_replace("'","",$v);
$v =  str_replace("\"","",$v);
$_GET[$key] = $v;
}
}
}
 
function ConnectDb()
{
LoadAPI("databaseLite");
$GLOBALS['DB'] = new databaseLite( DB_HOST , DB_USER , DB_PASS , DB_NAME , DB_ENCODE );
$GLOBALS['DB']->debug = 1;
}
 
function checkFile( $file )
{
if(!is_file($file))
{
$MOD_params['error_message'] = "载入指定文件失败 $file";
DoException($MOD_params);
}
}
 
function __autoload($class_name)
{
LoadAPI($class_name);
}
 
function LoadAPI($p_api_name)
{
$apiFile = GA_ROOT."/api/".$p_api_name.".php";
if(!is_file($apiFile))
{
$MOD_params['error_message'] = "载入指定类库失败 $p_api_name";
DoException($MOD_params);
}
require_once( $apiFile );
}
 
function LoadAdminModel($p_mod_name)
{
$modFile = GA_ROOT."/admin/model/M_".$p_mod_name.".php";
if(!is_file($modFile))
{
$MOD_params['error_message'] = "载入指定模块失败 $p_mod_name";
DoException($MOD_params);
}
require_once( $modFile );
}
 
function LoadModel($p_mod_name,$pindao="")
{
//分频道
//update 2011-09-22 by huaweiwei
if($pindao){
$modFile = GA_ROOT."/".$pindao."/model/M_".$p_mod_name.".php";
}else{
$modFile = GA_ROOT."/model/M_".$p_mod_name.".php";
}
 
if(!is_file($modFile))
{
$MOD_params['error_message'] = "载入指定模块失败 $p_mod_name";
DoException($MOD_params , true);
}
require_once( $modFile );
}
 
function DoAjaxException($msg)
{
exit("1|$msg");
}
 
function DoException( $MOD_params , $channel='default')
{
if(MOD_TYPE=='front')
$modPath = MOD_PATH;
else
$modPath = ADMIN_MOD_PATH;
if(MOD_TYPE=='front')require_once( $modPath."/model.php" );
 
$mod_file = $modPath."/M_error.php";
require_once( $mod_file );
 
if(!is_array($MOD_params))
{
$params = array('error_message'=>$MOD_params);
$params = array('msg'=>$MOD_params);
$MOD_params = $params;
}
 
echo file_get_contents("../404page/index.html");
die();
$M = new M_error();
$M->setParams($MOD_params);
if(MOD_TYPE=='front')
$M->showMessage($channel);
else
$M->showMessage();
exit;
}
function f_put2($file , $data , $mode="wb" , $autoMkDir=true , $showFileName=true )
{
f_put( $file , $data , $mode , $autoMkDir , $showFileName );
}
function f_put( $file , $data , $mode="wb" , $autoMkDir=false , $showFileName=false )
{
$fp = @fopen($file,$mode);
if(!$fp && $autoMkDir)
{
$path = dirname($file);
if(!is_dir($path))
{
mkdirp($path);
}
$fp = @fopen($file,$mode);
}
@fwrite($fp,$data);
@fclose($fp);
if($showFileName)
{
$vfile =  str_replace(A,"",$file);
echo "<a href='$vfile' target=_blank>$vfile</a>";
if($fp)echo "--ok<br>\n";
else echo "--<font color=red>失败</font><br>\n";
}
}
 
//直接建立多层目录
function mkdirp($target) {
if (file_exists($target)) {
if (!is_dir($target)) return false;
else return true;
}
if ( @mkdir($target) ) return true;
if ( mkdirp(dirname2($target)) ) return mkdirp($target);
return false;
}
 
function pager( $DB , $table , $where , &$tpl , $limit=30 , $p=1 , $baseLink="/index.php" , $total=0 ,$ajax=false,$show=true)
{
if($ajax){
LoadAPI("pageNavProajax");
}
else{
LoadAPI("pageNavPro");
}
if(!$p)
{
$p = intval($_GET['p']);
if($p<=0)$p=1;
}
//计算开始条数
$limitstart = ($p-1)*$limit;
//统计共有记录数
if(!$total)
{
$q2 = "SELECT count(*) FROM ".$table." $where" ;
 
if($num=0){
return ;
}
$DB->setQuery( $q2 );
$total = $DB->loadResult();
}
if($limitstart>=$total )
{
$limitstart = $total-$total%$limit;
if($total>0)
die("参数错误,无此页码");
}
if( $limitstart<0 )$limitstart=0;
$tpl->set( "baseLink" , $baseLink );
if($total>0)
{
$nav = new pageNav( $p , $limit , $total );
$nav->pageLink = $baseLink;
$pageLinks = $nav->numPage($total,$show);
$thispage = max($nav->page,1);
 
$tpl->set( "pageLinks",$pageLinks);
$tpl->set( "p",$thispage);
$tpl->set( "totalPages",$nav->totalPages);
}
else
{
$tpl->set( "p",1);
$tpl->set( "totalPages",1);
}
$tpl->set( "total",$total);
//echo $limitstart;
return array( $total , $limitstart , $limit );
}
 
 
function pr($row)
{
echo "<pre>";
print_R($row);
}
 
function prt($arr,$cols=80,$rows=10)
{
echo "<TEXTAREA  cols='$cols' rows='$rows'>";
print_R($arr);
echo "</textarea>";
}
 
function highlightString(&$string , $findKeyArr ,$highlightTagStart="<font color=red>", $highlightTagEnd="</font>" )
{
 
$start = "[[<<{{";
$end = "}}>>]]";
//pr($_GET);
//pr($findKeyArr);die;
//替换key
foreach( $findKeyArr as $k=>$key)
{
if ($key=="")
return;
 
$key = preg_quote( $key , "#" );
$reg = "#".$key."#i";
$string = preg_replace( $reg, $start."\$0".$end,$string);
}
$string = str_replace($start,$highlightTagStart,$string);
$string = str_replace($end,$highlightTagEnd,$string);
}
?>


<?php
 
function tableSelect( $table , $formItemName , $default , $keyCol="id",$valueCol="title" , $pkey="pid" , $optgroup=false )
{
    global $DB;
LoadAPI("mosHtml");
if($pkey&&$optgroup)
{
$q="select $keyCol , $valueCol , $pkey from $table";
$DB->setQuery($q);
$rows = $DB->loadList($keyCol);
foreach( $rows as $row )
{
$dwRows[$row->$pkey][$row->$keyCol]=$row->$valueCol;
$titleArr[$row->$keyCol] = $row->$valueCol;
}
$params = array('tag_name'=>$formItemName,'selected'=>$default,'mode'=>'','tag_attribs'=>'','withEmpty'=>'0','pkey'=>$pkey,'titleArr'=>$titleArr);
$select = html::optList( $dwRows , $params  );
return $select;
}
$q="select $keyCol , $valueCol from $table";
$DB->setQuery($q);
$arr = $DB->loadResultList($keyCol,$valueCol);
$params = array('tag_name'=>$formItemName,'selected'=>$default,'mode'=>'','tag_attribs'=>'','withEmpty'=>'1');
$select = mosHTML::selectList( $arr , $params  );
return $select;
}
 
 
//获取无限级联动的JS
function getDynaSelectJs( $dynaName , $divId , $formItemName , $defaultValue   )
{
$data .= "<div id='$divId'></div>\n";
$data .= "<script>var $dynaName = new recurDynaList('$dynaName','selectDynaDiv', '$formItemName', _ctitle , _cindex , _cpindex );\n";
$data .= "dynaSeltest.noself=true;dynaSeltest.init( '$defaultValue' );</script>\n";
return $data;
}
 
function ArrayToPhp( $arrayName , $array )
{
    $str  = "<?php";
$str .= "\n\$".$arrayName." = ".ArrayToString($array).";";
$str .= "\n?>";
return $str;
}
 
function ArrayToJson($obj,$withKey=true,$topWithKey=true,$isTop=false)
{
if(empty($obj)) return "{}";
$objType=gettype($obj);
if ($objType=='array') {
$objstring = "{";
   foreach ($obj as $objkey=>$objv) {
if($withKey || $isTop&&$topWithKey)$objstring .="\"$objkey\":";
$vtype =gettype($objv) ;
if ($vtype=='integer') {
 $objstring .="$objv,";
}else if ($vtype=='double'){
 $objstring .="$objv,"; 
}else if ($vtype=='string'){
 $objv= str_replace('"',"\\\"",$objv);
 $objstring .="\"".$objv."\","; 
}else if ($vtype=='array'){
 $objstring .="".ArrayToJson($objv,$withKey).",";
}else if ($vtype=='object'){
 $objstring .="".ArrayToJson($objv,$withKey).","; 
}else {
 $objstring .="\"".$objv."\","; 
}
   }
$objstring = substr($objstring,0,-1)."";
return $objstring."}\n";
}
}
 
function ArrayToString($obj,$withKey=true){
if(empty($obj)) return "array()";
$objType=gettype($obj);
if ($objType=='array') {
$objstring = "array(";
   foreach ($obj as $objkey=>$objv) {
if($withKey)$objstring .="\"$objkey\"=>";
$vtype =gettype($objv) ;
if ($vtype=='integer') {
 $objstring .="$objv,";
}else if ($vtype=='double'){
 $objstring .="$objv,"; 
}else if ($vtype=='string'){
 $objv= str_replace('"',"\\\"",$objv);
 $objstring .="\"".$objv."\","; 
}else if ($vtype=='array'){
 $objstring .="".ArrayToString($objv,$withKey).","; 
}else if ($vtype=='object'){
 $objstring .="".ArrayToString($objv,$withKey).","; 
}else {
 $objstring .="\"".$objv."\","; 
}
   }
$objstring = substr($objstring,0,-1)."";
return $objstring.")\n";
}
}
 
 
 
/*
$data=getRemotePage( $url , "i:/gathercache2/bookgif" , 1 );
prt($data);
f_put("i:/badxx.gif",$data);
*/
 
function getRemotePage( $url , $dir='netget' , $time=false )
{
global $fk;
if(!$url)return '';
$md5=md5($url);
$subdir=substr($md5,0,1)."/".substr($md5,1,1);
if(strpos($dir,"/")||strpos($dir,":"))
{
$local=$dir."/$subdir/".$md5.".cae";
}
else 
{
$local = A."/cacheurl/$dir/$subdir/". $md5.".cae";
}
if(CURL_DEBUG==1)echo $url ."<br>\n". $local."<br>\n";
if( is_file($local) )
{
if(!$time)
return file_get_contents ( $local );
else
{
if((filemtime($local)+$time)>time())
{
return file_get_contents ( $local );
}
}
}
 
echo "huoqu <br>\n";
if($fk)
{
$data = $fk->f_get( $url , "" , $url );
}
elseif (function_exists('curl_init'))
{
   $data = curl_get($url);
}
else
$data = file_get_contents ( $url );
 
if($data)
{
if( !$time || $time>3 )
$rt = f_put ( $local , $data , "wb" , true );
//记录日志
$size = strlen($data);
$KB = intval($size/1024);
//f_put( A."/cache/getRemotePage.txt" , date('Y-m-d H:i:s')."\t". $url."\t$size\t".$KB."\n" , "a+" );
}
else
{
if(CURL_DEBUG==1)
{
echo "no data <br>\n";
}
}
return $data;
}
 
function curl_get ($url)
global $useproxy;
if(CURL_DEBUG==1)echo $url ."=curl used<br>\n";
if(strtolower(substr($url,0,7))!="http://")return;
$ch = curl_init(); 
curl_setopt ( $ch, CURLOPT_TIMEOUT, 0); 
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT , 5);
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1); 
$header=curlheader();
if($header)curl_setopt ($ch, CURLOPT_HTTPHEADER , $header ); 
curl_setopt ($ch, CURLOPT_URL , $url);
curl_setopt ($ch, CURLOPT_REFERER , $url );
if($useproxy)
{
if(CURL_DEBUG==1)echo "使用代理<br>\n";
curl_setopt ($ch, CURLOPT_PROXY, "119.147.242.51");//218.75.135.188
curl_setopt ($ch, CURLOPT_PROXYPORT,"52333");
}
//if(CURL_DEBUG==1)curl_setopt($ch, CURLOPT_VERBOSE, 1);
//curl_setopt ($ch, CURLOPT_COOKIE , $cookie );
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
$data=curl_exec ($ch); 
//if(CURL_DEBUG==1){$info = curl_getinfo($ch);prt($info);}
curl_close($ch);
return $data;
//echo(curl_error($ch)) ."<br>\n";
/*
if (empty($info['http_code'])) {
  //echo("No HTTP code was returned"); 
} else {
}
*/
}
 
function curlheader()
{
$header = array();
$header[] ="Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, */*";
$header[] ="Accept-Language: zh-cn";
//$header[] ="Accept-Encoding: gzip, deflate";
$header[] ="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
$header[] ="Connection: Keep-Alive";
return $header;
}
 
 
//目录拷贝 , ignore格式 /目录名/目录名2/目录名3/
function dircopy($srcdir, $dstdir,$ignore="", $verbose = false) {
  $num = 0;
  if(!is_dir($dstdir)) mkdir($dstdir);
  if($curdir = opendir($srcdir)) {
if(!$ignore)$ignoreNow="/./../";
else 
{
  $ignoreNow=$ignore."./../";
}
   while($file = readdir($curdir)) {
  $igfile = "/$file/";
  if(strpos($ignoreNow,$igfile)!==false)continue;
       $srcfile = $srcdir . '\\' . $file;
       $dstfile = $dstdir . '\\' . $file;
       if(is_file($srcfile)) {
         if(is_file($dstfile)) $ow = filemtime($srcfile) - filemtime($dstfile); else $ow = 1;
         if($ow > 0) {
           if($verbose) echo "Copying '$srcfile' to '$dstfile'...";
           if(copy($srcfile, $dstfile)) {
             touch($dstfile, filemtime($srcfile)); $num++;
             if($verbose) echo "OK\n";
           }
           else echo "Error: File '$srcfile' could not be copied!\n";
         }                  
       }
       else if(is_dir($srcfile)) {
         $num += dircopy($srcfile, $dstfile,$ignore, $verbose);
       }
   }
   closedir($curdir);
  }
  return $num;
}
 
//后台需要的sysFunc.php
 
function getPinyin($word)
{
    LoadAPI('fy');
$py = new PinyinClass;
return $py->c_all($word);
}
 
 
//根据代入的sql...,返回php代码
function getPhp($sql,$mainKey='0',$valueKey='',$filter=false)
{
    global $DB;
$DB->setQuery($sql);
$rows = $DB->loadResultList( $mainKey , $valueKey );
//pr($rows);
if($filter)$rows = array_unique($rows);
return ArrayToString($rows);
}
 
//根据代入的table,id,title,返回select列表
function getSelectList($tagName,$selected,$table,$where=" WHERE 1",$id='id',$title='title')
{
    global $DB;
LoadAPI( "mosHtml" );
$sql="select $id,$title from $table $where";
$DB->setQuery($sql);
$rows = $DB->loadResultList($id,$title);
$params = array('tag_name'=>$tagName , 'selected'=>$selected );
return mosHTML::selectList( $rows , $params );
}
 
function e($str)
{
    global $DB;
return $DB->e($str);
}
 
//增强的explode
function Pexplode( $line , $secondSplit="=" , $spliter="&" ,  $decode=true  )
{
$r = array();
$arr = explode( $spliter , $line );
foreach( $arr as $item )
{
list($key,$v) = explode( $secondSplit , $item );
if ($decode)
{
$key = rawurldecode($key);
$v = rawurldecode($v);
}
$r[$key] = $v;
}
return $r;
}
 
//增强的implode
function Pimplode( $arr , $pattern="{{key}}={{value}}" , $spliter="&" ,  $encode=true , $filterArr = array()  )
{
$r = array();
foreach( $arr as $gkey=>$gv)
{
if (!in_array( $gkey , $filterArr ))
{
if ($encode)
{
$gkey = rawurlencode($gkey);
$gv = rawurlencode($gv);
}
$item = str_replace("{{key}}",$gkey,$pattern);
$item = str_replace("{{value}}",$gv,$item);
$r[] = $item;
}
}
$getstring = implode($spliter,$r);
return $getstring;
}
 
function pagerNoDB( &$tpl , $limit=30 , $p=1 , $baseLink="/index.php?p={{page}}" , $total=0 )
{
LoadAPI("pageNavPro");
if(!$p)
{
$p = intval($_GET['p']);
if($p<=0)$p=1;
}
//计算开始条数
$limitstart = ($p-1)*$limit; 
//统计共有记录数
if($limitstart>=$total )
{
$limitstart = $total-$total%$limit;
if($total>0)
die("参数错误,无此页码");
}
if( $limitstart<0 )$limitstart=0;
$tpl->set( "baseLink" , $baseLink );
if($total>0)
{
$nav = new pageNav( $p , $limit , $total );
$nav->pageLink = $baseLink;
$pageLinks = $nav->numPage($limitstart);
$thispage = max($nav->page,1);
$tpl->set( "pageLinks",$pageLinks);
$tpl->set( "p",$thispage);
$tpl->set( "totalPages",$nav->totalPages);
}
else
{
$tpl->set( "p",1);
$tpl->set( "totalPages",1);    
}
$tpl->set( "total",$total);
return $limitstart;
}
 
function getGroupRowsNum($table,$groupCol,$where="")
{
    global $DB;
$sql="select count(distinct $groupCol) from $table $where";
$DB->setQuery($sql);
$total = $DB->loadResult();
return $total;
}
 
function pagerPro( $DB , $table , $where , &$tpl , $selectbox=true , $link="/index.php" , $option=array())
{
LoadAPI("adminPager");
$p = intval($_GET['p']);
$getstring=_parsePost("_GET" , 'p' );
//定义条数
$limit = intval($_GET['limit']) ? intval($_GET['limit']) : LIMIT;
if($p<=0)$p=1;
//计算开始条数
$limitstart = ($p-1)*$limit;
 
extract($option);
if( !$total )
{
//统计共有记录数
$q2 = "SELECT count(*) FROM ".$table." $where" ;
$DB->setQuery( $q2 );
$total = $DB->loadResult();
}
 
if($limitstart>=$total )
{
$limitstart = $total-($total%$limit);
if( $limitstart==$total )
$limitstart = $total-$limit;
}
 
if( $limitstart<0 )$limitstart=0;
 
if($total>0)
{
$pageNav = new pageNav( $p , $limit , $total );
$link = $link."?".$getstring."&p=";
$pageNav->pageLink=$link."{{page}}";
$pageNav->selectPageLink= "'".$link."'+{{page}}";
$pageLinks   = $pageNav->getPagesLinks();
$pageCounter = $pageNav->getPagesCounter( $selectbox );
}
$limitEnd = $limitstart + $limit;
if ($limitEnd > $total) $limitEnd = $total;
 
$tpl->set( "limitStart" , ($limitStart+1) );
$tpl->set( "limitEnd" , $limitEnd );
$tpl->set( "p",$p);
$tpl->set( "totalPages",$totalPages);
$tpl->set( "total",$total);
$tpl->set( "pageLinks",$pageLinks);
$tpl->set( "pageCounter",$pageCounter);
return array( $total , $limitstart , $limit );
}
 
 
 
function pagerPro2( $DB , $table , $where , &$tpl , $selectbox=true , $link="/index.php")
{
LoadAPI("adminPager");
$p = intval($_GET['p']);
$getstring=_parsePost("_GET" , 'p' );
//定义条数
$limit = intval($_GET['limit']) ? intval($_GET['limit']) : LIMIT2;
if($p<=0)$p=1;
//计算开始条数
$limitstart = ($p-1)*$limit;
 
//统计共有记录数
$q2 = "SELECT count(*) FROM ".$table." $where" ;
$DB->setQuery( $q2 );
$total = $DB->loadResult();
 
if($limitstart>=$total )
{
$limitstart = $total-($total%$limit);
if( $limitstart==$total )
$limitstart = $total-$limit;
}
 
if( $limitstart<0 )$limitstart=0;
 
if($total>0)
{
$pageNav = new pageNav( $p , $limit , $total );
$link = $link."?".$getstring."&p=";
$pageNav->pageLink=$link."{{page}}";
$pageNav->selectPageLink= "'".$link."'+{{page}}";
$pageLinks   = $pageNav->getPagesLinks();
$pageCounter = $pageNav->getPagesCounter( $selectbox );
}
$limitEnd = $limitstart + $limit;
if ($limitEnd > $total) $limitEnd = $total;
 
$tpl->set( "limitStart" , ($limitStart+1) );
$tpl->set( "limitEnd" , $limitEnd );
$tpl->set( "p",$p);
$tpl->set( "totalPages",$totalPages);
$tpl->set( "total",$total);
$tpl->set( "pageLinks",$pageLinks);
$tpl->set( "pageCounter",$pageCounter);
return array( $total , $limitstart , $limit );
}
 
function cut($str,$from,$to='',$direct='out',$err=false)
{
//echo "$str \n $from \n $to \n"; //$from = "\""; $to = "\"";
$frompos = strpos($str,$from); //f_put( "f:/".md5($from),$str); echo $from ."<br>\n"; echo $frompos ."<br>\n";
if($to!='')
{
$topos = strpos($str,$to,$frompos+strlen($from));
if (!$topos)
{
//prt($from);
//prt($str);
//prt($to);
//echo($frompos);
}
}
if($err&&(!$frompos||!$topos))
{
return false;
}
if($direct == 'in')
{
$start = $frompos+strlen($from);
if($to!='')
$end = $topos-$start;
}
else 
{
$start = $frompos;
if($to!='')
$end   = $topos+strlen($to)-$frompos;
}
if($to!='')
$txt = substr($str,$start,$end);
else
$txt = substr($str,$start);
return $txt;
}
 
//cut 数组版
function cutArray($content,$start,$end,$direct='out'){
@$m = explode($start,$content);
$a = array();
if ($m != '')
{
$sum = count($m);
for( $i = 1;$i < $sum;$i++ )
{
$my = explode($end,$m[$i]);
if($direct == 'in'){
$a[] = $start.$my[0].$end;
}
else{
$a[] = $my[0];
}
unset($my);
}
}
return $a;
}
?>

<?php
//文件目录操作相关函数
 
//不出错的dirname
function dirname2($dir)
{
//echo $file;
$pos = strrpos($dir,"/");
if(false===$pos)
$pos = strrpos($dir,"\\");
if(false===$pos)
return '';
if($pos===0)$pos =1;
return substr( $dir , 0 , $pos );
}
 
//echo Basename2( "f:/zz.rar", '.rar' );
function Basename3( $file )
{
    $extname = ExtName( $file );
return Basename2( $file , ".".$extname );
}
 
//不出错的basename
function Basename2( $file , $extname='' )
{
$file = str_replace("\\\\","/",$file);
$file = str_replace("//","/",$file);
 
//echo $file;
 
$pos = strrpos($file,"/");
if(false===$pos)
$pos = strrpos($file,"\\");
if(false===$pos)$pos=-1;
if ($extname!='')
{
return substr( $file , $pos+1 , -strlen($extname) );   
}
return substr($file , $pos+1);
}
 
function ExtName( $file )
{
$pos = strrpos($file,".");
if($pos===false)
{
return $pos;
}
return substr($file , $pos+1);
}
function isEmptyDir(   $path   )
{   
 $dh=   opendir(   $path   );   
 while(false   !==   ($f   =   readdir($dh)))   
 {   
if($f   !=   "."   &&   $f   !=   ".."   )   
 return   false;   
 }   
 return   true;
}
?>

普通分类: