php实用小代码

1、实现中文字符串截取无乱码方法
开启mbstring扩展,然后自定义函数:
<?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)
);
}
//实例
$str = "我是一个good男孩!";
echo substr_utf8($str, 2, 4);
2、用PHP打印前一天的时间
<?php
header('content-Type:text/html:charset=utf-8');
echo date('Y-m-d H:i:s',strtotime('-1 day'));
3、不适用第三个变量交换2个变量的值
<?php
header('content-Type:text/html:charset=utf-8');
$a = 'a';
$b = 'b';
list($a,$b) = array($b,$a);
echo $a,$b;
4、将1234567890,转换成1,234,567,890
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;
5、实现utf8字符串反转
不能使用strrev,中文会出错
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));
}
7、写一个函数,将字符串open_door转换为OpenDoor
$str = "open_door";
function changestr($str){
$arr = explode('
',$str);
$arr = array_map('ucfirst',$arr);
return implode('',$arr);
}
echo change_str($str);
8、单例模式
<?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
$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];
11、写一个函数可以便利一个文件夹下的所有文件和子文件夹
<?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'));
12、论坛中无限分类实现原理
首先设计数据库表
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
//然后以此节点为根几点,遍历其子节点
if($v['parent_id'] == $pid){
$v['level'] = $level;
$list[] = $v;
tree($arr,$v['cat_id'],$level+1);
}
}
return $list;
}
13、计算2个文件的相对路径
<?php
$a = '/a/b/c/d/a.php';
$b = '/a/b/e/f/b.php';
$arr1 = explode('/',dirname($a));
$arr2 = explode('/',dirname($b));
for($i=0,$len=count($arr2);$i<$len;$i++){
if($arr1[$i] != $arr2[$i]){
break;
}
}
//不在用一个根目录
if($i == 1){
$ret = array();
}
//在同一个根目录下
if($i != 1 && $i < $len){
$ret = array_fill(0,$len-$i,"..");
}
//在同一个目录下
if($i == $len){
$ret = array('./');
}
$ret = array_merge($ret,array_slice($arr1,$i));
echo implode('/',$ret);
14、约瑟夫环问题
<?php
function king($n,$m){
$monkey = range(1,$n);
$i = 0;
while(count($monkey) > 1){
$i += 1;
$head = array_shift($monkey);//一个个出列最前面的
if( $i % $m != 0){
//如果不是m的倍数,则返回尾部,否则就出列了
array_push($monkey,$head);
}
}
return $monkey[0];
}
echo king(10,7);
15、PHP实现双向队列
<?php
class Dqueue{
private $queue = array();
public function addFirst($item){
return array_unshift($this->queue,$item);
}
public function addLast($item){
return array_push($this->queue,$item);
}
public function getFirst(){
return array_shift($this->queue);
}
public function getLast(){
return array_pop($this->queue);
}
}**

猜你喜欢

转载自blog.51cto.com/12955237/2107796