日期:2014-05-17 浏览次数:20715 次
<?php header('content-Type:text/html:charset=utf-8'); function substr_utf8($str, $start, $length = null) { return join("", array_slice( preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY), $start, $length) ); } //实例 (PS:^_^不错的php学习交流群:276167802,验证:csl,有兴趣的话可以加入进来一起讨论) $str = "我是一个good男孩!"; echo substr_utf8($str, 2, 4);
<?php header('content-Type:text/html:charset=utf-8'); echo date('Y-m-d H:i:s',strtotime('-1 day'));
<?php header('content-Type:text/html:charset=utf-8'); $a = 'a'; $b = 'b'; list($a,$b) = array($b,$a); echo $a,$b;
header('content-Type:text/html:charset=utf-8'); $str = '1234567890'; //反转字符串 $str = strrev($str); //使用逗号分隔得到098,765,432,1, $str = chunk_split($str,3,','); //再次反转 $str = strrev($str); //去掉左边的, $str = ltrim($str,','); echo $str;
function strrev_utf8($str){ return join("",array_reverse(preg_split("//u",$str))); } $str = "我是一个good男孩"; echo strrev_utf8($str); 6、取url的文件扩展名,尽量多的去实现方法 $str = "www.baidu.com/index.php"; function get_ext1($str){ return strrchr($str,'.'); } function get_ext2($str){ return substr($str,strrpos($str,'.')); } function get_ext3($str){ $str = pathinfo($str); return $str['extension']; } function get_ext4($str){ $arr = explode('.',$str); return $arr[count($arr)-1]; } function get_ext5($str){ $pattern = '/^[^\.]+\.([\w]+)$/'; return preg_replace($pattern,'${1}',basename($str)); }
$str = "open_door"; function change_str($str){ $arr = explode('_',$str); $arr = array_map('ucfirst',$arr); return implode('',$arr); } echo change_str($str);
<?php class Mysql{ private static $instance = null; private $conn; //设置为私有,不允许通过new获得对象 private function __construct(){ $conn = mysql_connect('localhost','root','123456'); } //获取实例方法 public static function getInstance(){ if(! self::$instance instanceof self){ self::$instance = new self; } return self::$instance; } //禁止克隆 private function __clone(){} } $db = Mysql::getInstance(); 9、写一段PHP代码,确保多个进程同时写入同一个文件成功 <?php $fp = fopen("lock.txt","w+"); if(flock($fp,LOCK_EX)){ //获得写锁 fwrite($fp,'write something'); flock($fp,LOCK_UN); }else{ echo "file is locking..."; } fclose($fp);
<?php $url = 'http://www.baidu.com/a/b/index.php?id=1'; $arr = parse_url($url); $fname = basename($arr['path']); $arr = explode('.',$fname); echo $arr[count($arr)-1];
<?php function my_scandir($dir){ $files = array(); if(is_dir($dir)){ if($handle = opendir($dir)){ while(($file = readdir($handle)) !== false){ if($file != "." && $file != ".."){ if(is_dir($dir.'/'.$file)){ $files[$file] = my_scandir($dir.'/'.$file); }else{ $files[] = $dir.'/'.$file; } } } closedir($handle); return $files; } } } var_dump(my_scandir('D:\wamp\www\study'));
create table category( cate_id int unsigned not null auto_increment primary key, cat_name varchar(30) not null default '', parent_id int unsigned not null default 0 ) engine=innodb charset=utf8; 然后用函数去递归实现,无限分类 function tree($arr,$pid=0,$level=0){ static $list = array(); foreach($arr as $v){ //如果是顶级分类,则存入$list //然后以