Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 网站建设 > PHP > PHP常用函数
【标  题】:PHP常用函数
【关键字】:PHP
【来  源】:http://blog.chinaunix.net/article.php?articleId=13742&blogId=4582

PHP常用函数

Your Ad Here 常用函数,不用说的啦.........

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)
    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;)/', '&\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;\n";

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

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

 $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 .= ");\n";
  }
 }

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

//////////

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

一些JAVASCRIPT:【上一篇】
linux下用php连接sqlserver解决办法:【下一篇】
【相关文章】
  • PHP-Push技术实现刷新功能 转载
  • I LOVE PHP!
  • php 分析rss代码一段
  • 关于PHP的分页代码
  • 简易投票系统 php+文本
  • win2k下Apache2+PHP4+MySql配置
  • 黑防黑:黑客口述—关于Php后门的隐藏技巧测试
  • php学习第一课---mysql的应用
  • php时间求法(二)
  • php与mysql三日通(三)
  • 【随机文章】
  • Web Services 好的网址
  • 什么是VPN技术
  • Red Hat Linux 7.1安装指南
  • 怎样在VC++中读取INI文件[转载]
  • 关于tomcat数据连接池问题
  • 狠狠撞击你的眼球
  • 控制对网站的访问
  • Photoshop 7.0 数字水印滤镜
  • ORACLE学习笔记2--ORACLE管理工具
  • 减少成本、改善客户服务,企业能做什么?
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.