一、PHP翻轉中文字符串url
function reverse($str){code
$r = array(); for($i=0; $i<mb_strlen($str); $i++){ $r[] = mb_substr($str, $i, 1, 'UTF-8'); } return implode(array_reverse($r));
}
echo reverse('');字符串
二、PHP計算URL的文件後綴名get
function getext($url){io
$data = parse_url($url); $path = $data['path']; $info = pathinfo($path); return $info['extension'];
}function
三、PHP計算兩個文件的相對路徑sed
function getrpath($path, $conpath){file
$pathArr = explode('/', $path); $conpathArr = explode('/', $conpath); $dismatchlen = 0; for($i=0; $i<count($pathArr); $i++){ if($conpathArr[$i] != $pathArr[$i]){ $dismatchlen = count($pathArr) - $i; $arrleft = array_slice($pathArr, $i); break; } } return str_repeat('../', $dismatchlen).implode('/', $arrleft);
}遍歷
四、PHP遍歷目錄下的全部文件和文件夾im
function finddir($dir){
$files = array(); if(is_dir($dir)){ if($handle = opendir($dir)){ while(($file = readdir($handle)) !== false){ if($file != '.' && $file != '..'){ if(is_dir(rtrim($dir, '/').'/'.$file)){ $files[$file] = finddir(rtrim($dir, '/').'/'.$file); }else{ $files[] = rtrim($dir, '/').'/'.$file; } } } closedir($handle); } } return $files;
}