一些常用的php函数_php基础

1.产生随机字符串函数 

<?php 
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使用有效 

<?php 
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地址 

<?php 
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.创建相应的文件夹 

<?php 
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); 
                } 
            } 
        } 

?>  

5.判断邮箱地址 

<?php 
function checkEmail($inAddress) 

 return (ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+",$inAddress)); 

?> 

6.跳转 

<?php 
function gotourl($message='',$url='',$title='') 

    $html  ="<html><head>"; 
    if(!empty($url)) 
     $html .="<meta http-equiv='refresh' content=\"3;url='".$url."'\">"; 
    $html .="<link href='../templates/style.css' type=text/css rel=stylesheet>"; 
    $html .="</head><body><br><br><br><br>"; 
    $html .="<table cellspacing='0' cellpadding='0' border='1' width='450' align='center'>"; 
 $html .="<tr><td bgcolor='#ffffff'>"; 
 $html .="<table border='1' cellspacing='1' cellpadding='4' width='100%'>"; 
 $html .="<tr class='m_title'>"; 
 $html .="<td>".$title."</td></tr>"; 
 $html .="<tr class='line_1'><td align='center' height='60'>"; 
 $html .="<br>".$message."<br><br>"; 
    if (!empty($url)) 
     $html .="系统将在3秒后返回<br>如果您的浏览器不能自动返回,请点击[<a href=".$url." target=_self>这里</a>]进入"; 
    else 
     $html .="[<a href='#' onclick='history.go(-1)'>返回</a>]"; 
    $html .="</td></tr></table></td></tr></table>"; 
 $html .="</body></html>"; 
 echo $html; 
 exit; 

?>  

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

<?php 
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="共<font color='red'><b>".$totalpage."</b></font>页  "; 
    $linkNum =4; 
    $start = ($page-round($linkNum/2))>0 ? ($page-round($linkNum/2)) : "1"; 
    $end   = ($page+round($linkNum/2))<$totalpage ? ($page+round($linkNum/2)) : $totalpage; 
    $prestart=$start-1; 
    $nextend=$end+1; 
    if($page<>1)  
 $out .= "<a href='?page=1&&".$string."'title=第一页>第一页</a> "; 
    if($start>1) 
 $out.="<a href='?page=".$prestart."' title=上一页>..<<</a> "; 
 for($t=$start;$t<=$end;$t++) 
    { 
     $out .= ($page==$t) ? "<font color='red'><b>[".$t."]</b></font> " : "<a href='?page=$t&&".$string."'>$t</a> "; 
    } 
 if($end<$totalpage) 
 $out.="<a href='?page=".$nextend."&&".$string."' title=下一页>>>..</a>"; 
    if($page<>$totalpage) 
   $out .= " <a href='?page=".$totalpage."&&".$string."' title=最后页>最后页</a>"; 
   return $out; 

?>  

时间: 2024-07-30 17:56:49

一些常用的php函数_php基础的相关文章

php常用hash加密函数_php技巧

本文实例讲述了php常用hash加密函数.分享给大家供大家参考.具体分析如下: 复制代码 代码如下: $hash_list=hash_algos();  //返回注册的hash规则列表 print_r($hash_list); //显示结果 创建文件以计算哈希值:file_put_contents('example.txt', 'the quick brown fox jumped over the lazy dog.'); 输出哈希值信息: 复制代码 代码如下: echo hash_file(

php学习笔记 数组的常用函数_php基础

复制代码 代码如下: <?php /* * 封装性:面向对象三大特性之一 * * 1.就是把对象的成员(属性,方法)结合成一个独立的相同单位,并尽可能隐藏对象的内部细节 * 访问权限修饰符 public protected private * private:私有的,用这个关键字修饰的成员,只能在对象内部访问(只有用$this访问) * * 属性可以封装: * 只要一个变量,需要在多个方法使用,就将这个变量声明为成员属性,可以直接在这个对象中的所有方法中使用 * * 成员属性,相当于这个对象中的

如何使用PHP中的字符串函数_php基础

如何使用PHP中的字符串函数 PHP中的字符串操作功能是比较多的,重要的有以下这些:    (1)echo,print,printf,sprintf   前两个函数是输出字符串.字符串中如果有变量名则被替换成其值.    后两个函数类似于C的同名函数.    (2)strchr,strlen,strtok,strrchr,strrev,strstr,strtolower,   strtoupper,substr,ucfirst   这些是常用的字符串操作函数,有些和C中的同名函数意义完全一致. 

模拟xcopy的函数_php基础

模拟xcopy的函数 <?php /************************************** 系统名称:模拟xcopy的函数* 程序功能:模拟xcopy的函数* 开发日期:2003/03/14*************************************/?><?//copy a direction's all files to another direction function xCopy($source, $destination, $child){

一个目录遍历函数_php基础

一个目录遍历函数 <?php function dirtree($path="./test") {  echo "<dl>";  $d = dir($path);  while(false !== ($v = $d->read())) {    if($v == "." $v == "..")      continue;    $file = $d->path."/".$v;

一个阿拉伯数字转中文数字的函数_php基础

最近因需要,写了个"阿拉伯数字转中文数字的函数".搜索了精华区只见到一个类似的.感觉到我的算法不错,所以贴出来共享一下如果要用于金额的转换,对小数部分的处理要做一下修改<?phpfunction ch_num($num,$mode=true) {  $char = array("零","壹","贰","叁","肆","伍","陆","

PHP 的几个配置文件函数_php基础

php 的配置函数就是几个ini_*的函数,主要是针对配置文件的操作,其实就四个函数:ini_get.ini_set.ini_get_all.ini_restore.个人感觉最有用的就是ini_set和ini_get.     * ini_get():获取配置文件的选项值     这个函数相信很多人都使过,就是获取配置文件中某一个选项的值,如果是true值就返回1,如果是false值就返回0,字符串就返回字符串.     比如手册中的例子:     〈?php     /*     Our ph

PHP操作mysql函数详解,mysql和php交互函数_php基础

1. 建立和关闭连接 1) mysql_connect() resource mysql_connect([string hostname [:port][:/path/to/socket][,string username] [,string password]]) 所有参数都是可选的 举例: @mysql_connect("localhost", "user", "password") or die("Could not conne

一个查看session内容的函数_php基础

之所以是能写出来这个函数,主要是对该网站的session结构清楚,如:name|s:4:"tasm";passwd|s:6:"111111";mode|s:1:"1",也知道该session存放的位置,而且可以上传文件,所以嘛,当时就做了一次小小的黑客,在线的朋友的密码可以一览无余,呵呵:<? function submit1(){ global $username; print "<title>论坛监听器</t