PHP 一些小技术

来源:互联网 发布:mac看bilibili发热 编辑:程序博客网 时间:2024/06/06 03:42
1、实现中文字符串截取无乱码方法
开启mbstring扩展,然后自定义函数:

[php] view plaincopy
  1. <?php  
  2. header('content-Type:text/html:charset=utf-8');  
  3. function substr_utf8($str$start$length = null) {  
  4. return join("",  
  5. array_slice(  
  6. preg_split("//u"$str, -1, PREG_SPLIT_NO_EMPTY), $start$length)  
  7. );  
  8. }  
  9. //实例 (PS:^_^不错的php学习交流群:276167802,验证:csl,有兴趣的话可以加入进来一起讨论)  
  10. $str = "我是一个good男孩!";  
  11. echo substr_utf8($str, 2, 4);  


2、用PHP打印前一天的时间


[php] view plaincopy
  1. <?php  
  2. header('content-Type:text/html:charset=utf-8');  
  3. echo date('Y-m-d H:i:s',strtotime('-1 day'));  


3、不适用第三个变量交换2个变量的值


[php] view plaincopy
  1. <?php  
  2. header('content-Type:text/html:charset=utf-8');  
  3. $a = 'a';  
  4. $b = 'b';  
  5. list($a,$b) = array($b,$a);  
  6. echo $a,$b;  


4、将1234567890,转换成1,234,567,890


[php] view plaincopy
  1. header('content-Type:text/html:charset=utf-8');  
  2. $str = '1234567890';  
  3. //反转字符串  
  4. $str = strrev($str);  
  5. //使用逗号分隔得到098,765,432,1,  
  6. $str = chunk_split($str,3,',');  
  7. //再次反转  
  8. $str = strrev($str);  
  9. //去掉左边的,  
  10. $str = ltrim($str,',');  
  11. echo $str;  


5、实现utf8字符串反转
不能使用strrev,中文会出错


[php] view plaincopy
  1. function strrev_utf8($str){  
  2. return join("",array_reverse(preg_split("//u",$str)));  
  3. }  
  4. $str = "我是一个good男孩";  
  5. echo strrev_utf8($str);  
  6. 6、取url的文件扩展名,尽量多的去实现方法  
  7. $str = "www.baidu.com/index.php";  
  8. function get_ext1($str){  
  9. return strrchr($str,'.');  
  10. }  
  11. function get_ext2($str){  
  12. return substr($str,strrpos($str,'.'));  
  13. }  
  14. function get_ext3($str){  
  15. $str = pathinfo($str);  
  16. return $str['extension'];  
  17. }  
  18. function get_ext4($str){  
  19. $arr = explode('.',$str);  
  20. return $arr[count($arr)-1];  
  21. }  
  22. function get_ext5($str){  
  23. $pattern = '/^[^\.]+\.([\w]+)$/';  
  24. return preg_replace($pattern,'${1}',basename($str));  
  25. }  


7、写一个函数,将字符串open_door转换为OpenDoor


[php] view plaincopy
  1. $str = "open_door";  
  2. function change_str($str){  
  3. $arr = explode('_',$str);  
  4. $arr = array_map('ucfirst',$arr);  
  5. return implode('',$arr);  
  6. }  
  7. echo change_str($str);  


8、单例模式


[php] view plaincopy
  1. <?php  
  2. class Mysql{  
  3. private static $instance = null;  
  4. private $conn;  
  5. //设置为私有,不允许通过new获得对象  
  6. private function __construct(){  
  7. $conn = mysql_connect('localhost','root','123456');  
  8. }  
  9. //获取实例方法  
  10. public static function getInstance(){  
  11. if(! self::$instance instanceof self){  
  12. self::$instance = new self;  
  13. }  
  14. return self::$instance;  
  15. }  
  16. //禁止克隆  
  17. private function __clone(){}  
  18. }  
  19. $db = Mysql::getInstance();  
  20. 9、写一段PHP代码,确保多个进程同时写入同一个文件成功  
  21. <?php  
  22. $fp = fopen("lock.txt","w+");  
  23. if(flock($fp,LOCK_EX)){  
  24. //获得写锁  
  25. fwrite($fp,'write something');  
  26. flock($fp,LOCK_UN);  
  27. }else{  
  28. echo "file is locking...";  
  29. }  
  30. fclose($fp);  


10、从一个完成的url获取文件扩展名


[php] view plaincopy
  1. <?php  
  2. $url = 'http://www.baidu.com/a/b/index.php?id=1';  
  3. $arr = parse_url($url);  
  4. $fname = basename($arr['path']);  
  5. $arr = explode('.',$fname);  
  6. echo $arr[count($arr)-1];  


11、写一个函数可以便利一个文件夹下的所有文件和子文件夹
[php] view plaincopy
  1. <?php  
  2. function my_scandir($dir){  
  3. $files = array();  
  4. if(is_dir($dir)){  
  5. if($handle = opendir($dir)){  
  6. while(($file = readdir($handle)) !== false){  
  7. if($file != "." && $file != ".."){  
  8. if(is_dir($dir.'/'.$file)){  
  9. $files[$file] = my_scandir($dir.'/'.$file);  
  10. }else{  
  11. $files[] = $dir.'/'.$file;  
  12. }  
  13. }  
  14. }  
  15. closedir($handle);  
  16. return $files;  
  17. }  
  18. }  
  19. }  
  20. var_dump(my_scandir('D:\wamp\www\study'));  


12、论坛中无限分类实现原理
首先设计数据库表


[php] view plaincopy
  1. create table category(  
  2. cate_id int unsigned not null auto_increment primary key,  
  3. cat_name varchar(30) not null default '',  
  4. parent_id int unsigned not null default 0  
  5. ) engine=innodb charset=utf8;  
  6. 然后用函数去递归实现,无限分类  
  7. function tree($arr,$pid=0,$level=0){  
  8. static $list = array();  
  9. foreach($arr as $v){  
  10. //如果是顶级分类,则存入$list  
  11. //然后以此节点为根几点,遍历其子节点  
  12. if($v['parent_id'] == $pid){  
  13. $v['level'] = $level;  
  14. $list[] = $v;  
  15. tree($arr,$v['cat_id'],$level+1);  
  16. }  
  17. }  
  18. return $list;  
  19. }  


13、计算2个文件的相对路径
[php] view plaincopy
  1. <?php  
  2. $a = '/a/b/c/d/a.php';  
  3. $b = '/a/b/e/f/b.php';  
  4. $arr1 = explode('/',dirname($a));  
  5. $arr2 = explode('/',dirname($b));  
  6. for($i=0,$len=count($arr2);$i<$len;$i++){  
  7. if($arr1[$i] != $arr2[$i]){  
  8. break;  
  9. }  
  10. }  
  11. //不在用一个根目录  
  12. if($i == 1){  
  13. $ret = array();  
  14. }  
  15. //在同一个根目录下  
  16. if($i != 1 && $i < $len){  
  17. $ret = array_fill(0,$len-$i,"..");  
  18. }  
  19. //在同一个目录下  
  20. if($i == $len){  
  21. $ret = array('./');  
  22. }  
  23. $ret = array_merge($ret,array_slice($arr1,$i));  
  24. echo implode('/',$ret);  
  25. 14、约瑟夫环问题  
  26. <?php  
  27. function king($n,$m){  
  28. $monkey = range(1,$n);  
  29. $i = 0;  
  30. while(count($monkey) > 1){  
  31. $i += 1;  
  32. $head = array_shift($monkey);//一个个出列最前面的  
  33. if$i % $m != 0){  
  34. //如果不是m的倍数,则返回尾部,否则就出列了  
  35. array_push($monkey,$head);  
  36. }  
  37. }  
  38. return $monkey[0];  
  39. }  
  40. echo king(10,7);  


15、PHP实现双向队列
[php] view plaincopy
  1. <?php  
  2. class Dqueue{  
  3. private $queue = array();  
  4. public function addFirst($item){  
  5. return array_unshift($this->queue,$item);  
  6. }  
  7. public function addLast($item){  
  8. return array_push($this->queue,$item);  
  9. }  
  10. public function getFirst(){  
  11. return array_shift($this->queue);  
  12. }  
  13. public function getLast(){  
  14. return array_pop($this->queue);  
  15. }  
  16. }   
0 0
原创粉丝点击