首页 > 后端开发 > php教程 > 经验分享 总结PHP常用函数_PHP教程

经验分享 总结PHP常用函数_PHP教程

WBOY
发布: 2016-07-15 13:32:05
原创
795 人浏览过

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

<ol class="dp-xml">
<li class="alt"><span><span>function wordscut($string, $length ,$</span><span class="attribute">sss</span><span>=</span><span class="attribute-value">0</span><span>) {  </span></span></li>
<li>
<span>if(strlen($string) </span><span class="tag">></span><span> $length) {  </span>
</li>
<li class="alt"><span>if($sss){  </span></li>
<li>
<span>$</span><span class="attribute">length</span><span>=$length - 3;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">addstr</span><span>=@# ...@#;  </span>
</li>
<li><span>}  </span></li>
<li class="alt">
<span>for($</span><span class="attribute">i</span><span> = </span><span class="attribute-value">0</span><span>; $i </span><span class="tag"><span> $length; $i++) {  </span></span>
</li>
<li>
<span>if(ord($string[$i]) </span><span class="tag">></span><span> 127) {  </span>
</li>
<li class="alt">
<span>$wordscut </span><span class="attribute">.</span><span>= $string[$i].$string[$i + 1];  </span>
</li>
<li><span>$i++;  </span></li>
<li class="alt"><span>} else {  </span></li>
<li>
<span>$wordscut </span><span class="attribute">.</span><span>= $string[$i];  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>return $wordscut.$addstr;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>return $string;  </span></li>
<li><span>}  </span></li>
</ol>
登录后复制

PHP常用函数3.取得客户端ip地址
 

<ol class="dp-xml">
<li class="alt"><span><span>function getip(){  </span></span></li>
<li><span>if (getenv("http_client_ip") <br>&& strcasecmp(getenv("http_client_ip"), "unknown"))  </span></li>
<li class="alt">
<span>$</span><span class="attribute">ip</span><span> = </span><span class="attribute-value">getenv</span><span>("http_client_ip");  </span>
</li>
<li><span>else if (getenv("http_x_forwarded_for") <br>&& strcasecmp(getenv("http_x_forwarded_for"), "unknown"))  </span></li>
<li class="alt">
<span>$</span><span class="attribute">ip</span><span> = </span><span class="attribute-value">getenv</span><span>("http_x_forwarded_for");  </span>
</li>
<li><span>else if (getenv("remote_addr")<br> && strcasecmp(getenv("remote_addr"), "unknown"))  </span></li>
<li class="alt">
<span>$</span><span class="attribute">ip</span><span> = </span><span class="attribute-value">getenv</span><span>("remote_addr");  </span>
</li>
<li><span>else if (isset($_server[@#remote_addr@#])<br> && $_server[@#remote_addr@#] <br>&& strcasecmp($_server[@#remote_addr@#], "unknown"))  </span></li>
<li class="alt">
<span>$</span><span class="attribute">ip</span><span> = $_server[@#remote_addr@#];  </span>
</li>
<li><span>else  </span></li>
<li class="alt">
<span>$</span><span class="attribute">ip</span><span> = </span><span class="attribute-value">"unknown"</span><span>;  </span>
</li>
<li><span>return($ip);  </span></li>
<li class="alt"><span>}  </span></li>
</ol>
登录后复制

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

<ol class="dp-xml">
<li class="alt"><span><span>function createdir($</span><span class="attribute">dir</span><span>=@#@#)  </span></span></li>
<li><span>{  </span></li>
<li class="alt"><span>if (!is_dir($dir))  </span></li>
<li><span>{  </span></li>
<li class="alt">
<span>$</span><span class="attribute">temp</span><span> = </span><span class="attribute-value">explode</span><span>(@#/@#,$dir);  </span>
</li>
<li>
<span>$</span><span class="attribute">cur_dir</span><span> = @#@#;  </span>
</li>
<li class="alt">
<span>for($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><span class="tag-name">count</span><span>($temp);$i++)  </span></span>
</li>
<li><span>{  </span></li>
<li class="alt">
<span>$cur_dir </span><span class="attribute">.</span><span>= $temp[$i].@#/@#;  </span>
</li>
<li><span>if (!is_dir($cur_dir))  </span></li>
<li class="alt"><span>{  </span></li>
<li><span>@mkdir($cur_dir,0777);  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>}  </span></li>
</ol>
登录后复制


 


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446157.htmlTechArticlePHP常用函数1.产生随机字符串函数 functionrandom($length){ $ hash =@#@#; $ chars =@#abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz@#; $ max = strlen ($ch...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板