经验分享 总结PHP常用函数

开发 后端
这篇文章中介绍的PHP常用函数包括产生随机字符串函数;截取一定长度的字符串;取得客户端ip地址;创建相应的文件夹等。

PHP的功能强大,主要是体现在了庞大的函数库上。在接下来的文章中,我们将为大家系统的讲解一些PHP常用函数,供大家参考学习。#t#

PHP常用函数1.产生随机字符串函数

  1. function random($length) {  
  2. $hash = @#@#;  
  3. $chars = @#abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz@#;  
  4. $max = strlen($chars) - 1;  
  5. mt_srand((double)microtime() * 1000000);  
  6. for($i = 0; $i < $length; $i++) {  
  7. $hash .= $chars[mt_rand(0, $max)];  
  8. }  
  9. return $hash;  
  10. }  

PHP常用函数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;  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

PHP常用函数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);   }  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

PHP常用函数4.创建相应的文件夹
 

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);  
}  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.


 

责任编辑:曹凯 来源: 百度博客
相关推荐

2011-07-11 10:24:09

PHP

2009-11-16 15:32:05

PHP数组函数

2009-12-07 11:11:46

PHP显示图片

2011-07-08 13:15:52

JSP

2009-11-17 11:24:00

PHP应用技巧

2009-12-08 18:11:42

PHP系统常量

2010-09-26 13:48:51

JVM调优

2015-03-10 11:30:00

MySQL日志维护

2011-07-10 15:07:37

2011-07-12 16:51:14

PHP

2009-12-09 13:11:03

PHP框架常见错误

2011-07-07 17:24:28

PHP

2011-07-07 17:27:54

PHP

2009-12-11 17:39:47

PHP String函

2009-12-11 17:45:09

PHP Math函数

2009-12-08 19:24:09

PHP函数索引

2009-11-16 10:57:51

PHP上传文件代码

2009-12-08 14:00:11

PHP函数microt

2014-04-02 10:56:21

2024-11-11 09:39:11

Cloudflare迁移Go
点赞
收藏

51CTO技术栈公众号