PHP函数自定义函数

  1. encrypt    实现对字符串进行加密,主要应用于用户注册模块中
    function keyED($txt,$encrypt_key){  
    	$encrypt_key = md5($encrypt_key);  
    	$ctr=0;  
    	$tmp = "";  
    	for ($i=0;$i<strlen($txt);$i++){  
    		if ($ctr==strlen($encrypt_key)){
    		 	$ctr=0;
    		 }
    		 else{
    			$tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);  
    		 }  
    	$ctr++;  
    	}  
    	return $tmp;  
    }  
    
    function encrypt($txt,$key){  
    	srand((double)microtime()*1000000);  
    	$encrypt_key = md5($key);
    	$ctr=0;  
    	$tmp ="";  
    	for ($i=0;$i<strlen($txt);$i++){  
    		if ($ctr==strlen($encrypt_key)){
    			 $ctr=0;
    		 }
    		 else{ 
    			$tmp.= substr($encrypt_key,$ctr,1) . (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));  
    		}
    	$ctr++; 
    	}  
    	return keyED($tmp,$key);  
    }  
    
    function decrypt($txt,$key){  
    	$txt = keyED($txt,$key);  
    	$tmp = "";  
    	for ($i=0;$i<strlen($txt);$i++){  
    		$md5 = substr($txt,$i,1);  
    		$i++;  
    		$tmp.= (substr($txt,$i,1) ^ $md5);  
    	}  
    	return $tmp;  
    }  
    
    <?php
    include("function.php");
    $txt="iwanc";
    $key="123456";
    $pass=encrypt($txt,$key);
    echo $pass.'<br />';
    echo decrypt($pass,$key);
    ?>
  2. unhtml    主要的作用是对提交的源代码进行处理,保证源代码以原形式进行输出,不会输出执行文件
    <style type="text/css">
    <!--
    body {
    	background-color: #EDDCDC;
    }
    .STYLE1 {font-size: 13px}
    -->
    </style><table width="600" border="2" cellpadding="2" cellspacing="2">
      <tr>
        <td bgcolor="#BFF9EB"><span class="STYLE1">
          <?php
    function unhtml($content){
        $content=htmlspecialchars($content);
        $content=str_replace(chr(13),"<br>",$content);
        $content=str_replace(chr(32),"&nbsp;",$content);
       return trim($content);
     }
     $content='
     <style type="text/css">
    <!--
    .style1 {font-size: 13px}
    -->
    </style>
    <table width="550" border="1" cellpadding="1" cellspacing="1" bgcolor="#15C4AE" class="style1">
      <tr>
        <td height="25" colspan="2" align="center">这是使用自定义函数unhtml的结果</td>
      </tr>
    </table>';
    echo unhtml($content);    //应用自定义函数unhtml
    ?>
        </span></td>
      </tr>
      <tr>
        <td align="center" class="STYLE1"><?php echo $content;?></td>
      </tr>
    </table>
  3. chinese_substr    按照指定的长度读取字符串,主要应用于实现对超长文本的分页显示
    <?php
    function chinese_substr($str,$start,$len){   
    	$strlen=$start+$len; 
        for($i=0;$i<$strlen;$i++) { 
           if(ord(substr($str,$i,1))>0xa0) { 
              $tmpstr.=substr($str,$i,2); 
              $i++;
            }else{
    		  $tmpstr.=substr($str,$i,1); } 
        } 
        return $tmpstr;	
    }
    $page=$_GET["page"];if ($page=="") {$page=1;};
    $counter="吉林省明日科技有限公司是一家以计算机软件技术为核心的高科技企业,多年来始终致力于行业管理软件开发、数字化出版物制作、计算机网络系统综合应用以及行业电子商务网站开发等领域,涉及生产、管理、控制、仓贮、物流、营销、服务等行业。公司拥有软件开发和项目实施方面的资深专家和学习型技术团队,多年来积累了丰富的技术文档和学习资源,公司的开发团队不仅是开拓进取的技术实践者,更致力于成为技术的普及和传播者。";
    $c=chinese_substr($counter,0,($page-1)*200);
    $c1=chinese_substr($counter,0,$page*200);
    echo substr($c1,strlen($c),strlen($c1)-strlen($c)); 
    ?>
    
     <?php
    if($page!=1){   
      echo  "<a href=index.php?page=1>首页</a>&nbsp;";
      echo "<a href=index.php?page=".($page-1).">上一页</a>&nbsp;";
     }
    if($page<$page_count){
      echo "<a href=index.php?page=".($page+1).">下一页</a>&nbsp;";
      echo  "<a href=index.php?page=".$page_count.">尾页</a>";				
    }				   
    ?> 
  4. china_date    应用于改变系统的当前时间
    <table windth="500" border="2" cellpadding="2" cellspacing="2" bgcolor="#208FFF">
    	<tr>
    		<td width="380" height="35" align="center">在标准的格林威志时间下获取的系统当前时间:</td>
    		<td width="186" align="left"><?php echo date("Y-m-d H:i:s");?></td>
    	</tr>
    	<tr>
    		<td width="380" height="35" align="center">使用东八区的时间获取的系统当前的时间:</td>
    		<td width="186" align="left"><?php 
    		function china_date(){
    			ini_set("date.timezone","Asia/Hong_Kong");
    		}
    		$now=china_date();
    		echo date("Y-m-d H:i:s");?></td>
    	</tr>
    </table>	
  5. mysql_gb2312    将数据库中的字符集转换成简体中文(GB2312),避免在输出数据库中的中文字符时发生乱码
    <?php $id=mysql_connect("localhost","root","root");
        mysql_select_db("db_database",$id);
    	function mysql_gb2312(){
           mysql_query("set names gb2312");
       }
    	$mysql=mysql_gb2312();
    ?>
  6. downfile    实现文件下载
    <?php
    function downfile(){
    	header("location:http://ww1.sinaimg.cn/bmiddle/7bc1501djw1dtn7ab9uvuj.jpg");
    }
    $downfile=downfile();
    ?>
  7. upload_file    实现文件上传
    <?php 
    function upload_file(){
    $data=date("Y-m-d");
    $file_name="file2";
    $path = './upfiles/'. $_FILES['file2']['name'];
    if (move_uploaded_file($_FILES['file2']['tmp_name'],$path)) { 
    	$query="insert into tb_file(file_name,file_text,data)values('$file_name','$path','$data')";
    	$result=mysql_query($query);
    	if($result=true){ 
    	echo "上传成功!!";
    	echo "<meta http-equiv=\"Refresh\" content=\"3;url=index.php?lmbs=文件上传\">"; 
    	}else{echo "文件上传失败!!";
              echo "<meta http-equiv=\"Refresh\" content=\"3;url=index.php?lmbs=文件上传\">";}
    }}
    
    if($Submit=="提交"){
    	$uploaded=upload_file();
    	}
    ?>
  8. make_seed    创建一组随机数字,以 microtime() 函数获取的当前时间的 UNIX 时间戳记的百万分之一秒值为原始数据
    <?php
    function make_seed() {
        list($seed, $seeds) = explode(' ', microtime());
        return (float) $seeds + ((float) $seed * 100000); }
        mt_srand(make_seed());
        $mt_rand = mt_rand();
    	$num=substr($mt_rand,1,5);
    for($i=0;$i<5;$i++){
    	echo "<img src=images/".substr(strval($num),$i,1).".gif>";
    }
    ?>
原文地址:https://www.cnblogs.com/iwanc/p/2654622.html