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

这里的技术是共享的

You are here

苏州人 各种 函数

shiping1 的头像
<?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";
    }
}



/*
$url="http://www.163.com/";
$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;
}
?>
普通分类: