APP NEWS

PHP常用函数推荐

这是一些使用频率比较高的函数,有的来自别人的程序...... 1.产生随机字符串函数functionrandom($length){ $hash=''; $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; $max=strlen($chars)-1; mt_s

2015-06-28 00:000 次阅读
PHP常用函数推荐

这是一些使用频率比较高的函数,有的来自别人的程序......

 

1.产生随机字符串函数

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;
}
 

2.截取一定长度的字符串

注:该函数对GB2312使用有效

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;
}
 

3.取得客户端IP地址

function GetIP(){
if (getenv(&HTTP_CLIENT_IP&) && strcasecmp(getenv(&HTTP_CLIENT_IP&), &unknown&))
$ip = getenv(&HTTP_CLIENT_IP&);
else if (getenv(&HTTP_X_FORWARDED_FOR&) && strcasecmp(getenv(&HTTP_X_FORWARDED_FOR&), &unknown&))
$ip = getenv(&HTTP_X_FORWARDED_FOR&);
else if (getenv(&REMOTE_ADDR&) && strcasecmp(getenv(&REMOTE_ADDR&), &unknown&))
$ip = getenv(&REMOTE_ADDR&);
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], &unknown&))
$ip = $_SERVER['REMOTE_ADDR'];
else
      $ip = &unknown&;
return($ip);
}

4.创建相应的文件夹

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

5.判断邮箱地址

function checkEmail($inAddress)
{
 return (ereg(&^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+&,$inAddress));

}

6.跳转

function gotourl($message='',$url='',$title='')
{
 
    $html  =&&;
    if(!empty($url))
     $html .=&&;
    $html .=&&;
    $html .=&



&;
    $html .=&

&;
 $html .=&
&;
 $html .=&&;
 $html .=&&;
 $html .=&&;
 $html .=&
&.$title.&
&;
 $html .=&
&.$message.&

&;
    if (!empty($url))
     $html .=&系统将在3秒后返回
如果您的浏览器不能自动返回,请点击[这里]进入&;
    else
     $html .=&[返回]&;
    $html .=&
&;
 $html .=&&;
 echo $html;
 exit;
}

7.分页(两个函数配合使用)

function getpage($sql,$page_size=20)
 {
      global $page,$totalpage,$sums;  //out param
      $page = $_GET[&page&];
      //$eachpage = $page_size;
      $pagesql = strstr($sql,& from &);
      $pagesql = &select count(*) as ids &.$pagesql;
      $result = mysql_query($pagesql);
      if($rs = mysql_fetch_array($result)) $sums = $rs[0];
      $totalpage = ceil($sums/$page_size);
      if((!$page)||($page<1)) $page=1;
   $startpos = ($page-1)*$page_size;
   $sql .=& limit $startpos,$page_size &;
    return $sql;
 }

function showbar($string=&&)
{    
    global $page,$totalpage;
 $out=&共&.$totalpage.&页  &;
    $linkNum =4;
    $start = ($page-round($linkNum/2))>0 ? ($page-round($linkNum/2)) : &1&;
&

关键词标签:推荐,函数,常用gt

相关阅读 2021年最好用的10款php开发工具推荐 plsql developer怎么连接数据库-plsql developer连接数据库方法 php利用淘宝IP库获取用户ip地理位置 在 PHP 中使用命令行工具 php出现Cannot modify header information问题的解决方法大全 详解ucenter原理及第三方应用程序整合思路、方法

文章评论

查看所有0条评论>>

发表评论

提交评论

热门文章 2021年最好用的10款php开发工具推荐 2021年最好用的10款php开发工具推荐 plsql developer怎么连接数据库-plsql developer连接数据库方法 plsql developer怎么连接数据库-plsql developer连接数据库方法 在 PHP 中使用命令行工具 在 PHP 中使用命令行工具 php应用程序安全防范技术研究 php应用程序安全防范技术研究 关于php curl获取301或302转向的网址问题 关于php curl获取301或302转向的网址问题

相关下载

人气排行 详解ucenter原理及第三方应用程序整合思路、方法 plsql developer怎么连接数据库-plsql developer连接数据库方法 PHP中防止SQL注入攻击 PHP会话Session的具体使用方法解析 PHP运行出现Notice : Use of undefined constant 的解决办法 PHP如何清空mySQL数据库 CakePHP程序员必须知道的21条技巧 PHP采集图片实例(PHP采集) 完美解决failed to open stream: HTTP request failed! 无法执行的解决办法 2021年最好用的10款php开发工具推荐 用header或meta实现PHP页面编码的区别

关于我们| 下载帮助| 版权声明| 合作伙伴| 友情连接| 联系我们| 网站地图

Copyright © 2007-2026 IT猫扑网 All Rights Reserved. 豫ICP备2026021191号-1 豫公网安备42010145200009号

{ "@context": "https://zhanzhang.baidu.com/contexts/cambrian.jsonld", "@id": "https://www.itmop.com/article/1099.html", "appid": "否", "title": "PHP常用函数推荐-IT猫扑网", "images": ["/e/data/images/notimg.gif"], "description": "{$Excerpt}", "pubDate": "2015-06-28T00:00:00", "upDate": "2015-06-28T00:00:00", "data": { "WebPage": { "pcUrl": "https://www.itmop.com/article/1099.html", "wapUrl": "https://m.itmop.com/article/1099.html", "fromSrc": "IT猫扑网" } } }
YOU MAY ALSO LIKE

更多应用推荐

浏览应用库