频道直达 - 专题 - 新闻 - 基础 - 高级 - 安装 - 技巧 - 数据库 - 手册 - PHP - Linux - Java - MySQL - Apache - 麻辣堂 - 狼盟 - Rails社群 - 搜索 - 下载 - 开源 - 论坛

PHP常用函数

来源:www.phpres.com 作者:Angelover 出处:www.phpres.com 2007-9-8 08:53:12 进入讨论组
关 键 词: 函数 常用 string if html filesize function return

常用函数,不用说的啦.........

 

function message($C_alert,$I_goback='') {
    if(!empty($I_goback)) {
        echo "<script>alert('$C_alert');window.location.href='$I_goback';</script>";
    } else {
        echo "<script>alert('$C_alert');</script>";
    }
}

/**
* 判断下拉菜音的选取项
*
* 可以判断字符串一和字符串二是否相等.从而使相等的项目在下拉菜单中被选择
*
* @access public
* @param string $str1  要比较的字符串一
* @param string $str2  要比较的字符串二
* @return string       相等返回字符串"selected",否则返回空字符串
*/
function selected($str1,$str2) {
    if($str1==$str2) {
        return ' selected';
    }
    return '';
}

/**
* 截取中文部分字符串
*
* 截取指定字符串指定长度的函数,该函数可自动判定中英文,不会出现乱码
*
* @access public
* @param string    $str    要处理的字符串
* @param int       $strlen 要截取的长度默认为10
* @param string    $other  是否要加上省略号,默认会加上
* @return string
*/
function showtitle($str,$strlen=10,$other=true) {
    $j = 0;
    for($i=0;$i<$strlen;$i++)
      if(ord(substr($str,$i,1))>0xa0) $j++;
    if($j%2!=0) $strlen++;
    $rstr=substr($str,0,$strlen);
    if (strlen($str)>$strlen && $other) {$rstr.='...';}
    return $rstr;
}

/////////////

function createdir($dir='')
{
        if (!is_dir($dir))
        {
            $temp = explode('/',$dir);
            $cur_dir = '';
            for($i=0;$i<count($temp);$i++)
            {
                $cur_dir .= $temp[$i].'/';
                if (!is_dir($cur_dir))
                {
                @mkdir($cur_dir,0777);
                }
            }
        }
}

////////////

function dhtmlspecialchars($string) {
 if(is_array($string)) {
  foreach($string as $key => $val) {
   $string[$key] = dhtmlspecialchars($val);
  }
 } else {
  $string = str_replace('&', '&amp;', $string);
  $string = str_replace('"', '&quot;', $string);
  $string = str_replace('<', '&lt;', $string);
  $string = str_replace('>', '&gt;', $string);
  $string = preg_replace('/&amp;(#d{3,5};)/', '&\1', $string);
 }
 return $string;
}

/////////

function daddslashes($string, $force = 0) {
 if(!$GLOBALS['magic_quotes_gpc'] || $force) {
  if(is_array($string)) {
   foreach($string as $key => $val) {
    $string[$key] = daddslashes($val, $force);
   }
  } else {
   $string = addslashes($string);
  }
 }
 return $string;
}

/////////.

function random($length) {
 $hash = '';
 $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
 $max = strlen($chars) - 1;
 mt_srand((double)microtime() * 1000000);
 for($i = 0; $i < $length; $i++) {
  $hash .= $chars[mt_rand(0, $max)];
 }
 return $hash;
}

//////

function wordscut($string, $length ,$sss=0) {
 if(strlen($string) > $length) {
               if($sss){
                      $length=$length - 3;
                      $addstr=' ...';
                }
  for($i = 0; $i < $length; $i++) {
   if(ord($string[$i]) > 127) {
    $wordscut .= $string[$i].$string[$i + 1];
    $i++;
   } else {
    $wordscut .= $string[$i];
   }
  }
  return $wordscut.$addstr;

 }
 return $string;
}

/////////

function sizecount($filesize) {
 if($filesize >= 1073741824) {
  $filesize = round($filesize / 1073741824 * 100) / 100 . ' G';
 } elseif($filesize >= 1048576) {
  $filesize = round($filesize / 1048576 * 100) / 100 . ' M';
 } elseif($filesize >= 1024) {
  $filesize = round($filesize / 1024 * 100) / 100 . ' K';
 } else {
  $filesize = $filesize . ' bytes';
 }
 return $filesize;
}

////////////////

function gotourl($message='',$url='')
{
 global $language;
    $html  ="<html><head>";
    if(!empty($url))
     $html .="<meta http-equiv='refresh' content="1;url='".$url."'">";
    $html .="<link href='./html/style.css' type=text/css rel=stylesheet>";
    $html .="</head><body><br><br><br><br>";
    $html .="<table cellspacing='0' cellpadding='0' border='0' width='450' align='center'>";
 $html .="<tr><td bgcolor='#000000'>";
 $html .="<table border='0' cellspacing='1' cellpadding='4' width='100%'>";
 $html .="<tr class='m_title'>";
 $html .="<td>".$language['messagebox_title']."</td></tr>";
 $html .="<tr class='line_1'><td align='center' height='60'>";
 $html .="<br>".$message."<br><br>";
    if (!empty($url))
     $html .="[<a href=".$url." target=_self>".$language['messagebox_exp_1']."</a>]";
    else
     $html .="[<a href='#' onclick='history.go(-1)'>".$language['messagebox_exp_2']."</a>]";
    $html .="</td></tr></table></td></tr></table>";
 $html .="</body></html>";
 echo $html;
 exit;
}

//////////////

function sqldumptable($table, $startfrom = 0, $currsize = 0) {
 global $db, $multivol, $sizelimit, $startrow;

 $offset = 64;
 if(!$startfrom)
    {
  $tabledump = "DROP TABLE IF EXISTS $table; ";

  $createtable = $db->query("SHOW CREATE TABLE $table");
  $create = $db->fetch_row($createtable);

  $tabledump .= $create[1]."; ";
  }

 $tabledumped = 0;
 $numrows = $offset;
 while(($multivol && $currsize + strlen($tabledump) < $sizelimit * 1000 && $numrows == $offset) || (!$multivol && !$tabledumped))
    {
  $tabledumped = 1;
  if($multivol)
        {
   $limitadd = "LIMIT $startfrom, $offset";
   $startfrom += $offset;
  }

  $rows = $db->query("SELECT * FROM $table $limitadd");
  $numfields = $db->num_fields($rows);
  $numrows = $db->num_rows($rows);
  while ($row = $db->fetch_row($rows))
        {
   $comma = "";
   $tabledump .= "INSERT INTO $table VALUES(";
   for($i = 0; $i < $numfields; $i++)
            {
    $tabledump .= $comma."'".mysql_escape_string($row[$i])."'";
    $comma = ",";
   }
   $tabledump .= "); ";
  }
 }

 $startrow = $startfrom;
 $tabledump .= " ";
 return $tabledump;
}

//////////

function splitsql($sql){
 $sql = str_replace(" ", " ", $sql);
 $ret = array();
 $num = 0;
 $queriesarray = explode("; ", trim($sql));
 unset($sql);
 foreach($queriesarray as $query) {
  $queries = explode(" ", trim($query));
  foreach($queries as $query) {
   $ret[$num] .= $query[0] == "#" ? NULL : $query;
  }
  $num++;
 }
 return($ret);
}

欢迎进入PHP开发资源论坛讨论。
收藏此文】【 】【打印】【关闭
相关文章
图文推荐
论 坛 资 源
PHP开发资源网奋斗目标
阅读排行:
热门技术文档
最新图文档
本站编辑推荐:(本站开通Delphi4PHP专区,欢迎进入论坛交流!)
编缉最近更新文章
网站赞助商
搜索您感兴趣的内容
 
   网站首页 -  网站地图 -  网站合作 -  手册中心 -  通用网址 -  网站论坛 -  网站投稿 -  友情链接 -  帮助中心
版权所有:PHP开发资源网 © 2003-2008 通用网址:PHP资源网 合作媒体: 赛迪网IT技术
互联网违法和不良信息举报中心 | 不良信息举报信箱