1. 在 HTML 语言中,页面头部的 meta 标记可以用来输出文件的编码格式,以下是一个标准的 meta 语句
请使用 PHP 语言写一个函数,把一个标准 HTML 页面中的类似 meta 标记中的 charset 部分值改为 big5
请注意:
(1) 需要处理完整的 html 页面,即不光此 meta 语句
(2) 忽略大小写
(3) ’ 和 ” 在此处是可以互换的
(4) ‘Content-Type’ 两侧的引号是可以忽略的,但 ‘text/html; charset=gbk’ 两侧的不行
(5) 注意处理多余空格
/** //www.jb51.net */
$html = "";
//匹配标准的meta标签
$pattern = "//i";
$replacement = "";
$result = preg_replace($pattern, $replacement, $html);
echo htmlspecialchars($result);
?>
2. 写一个函数,算出两个文件的相对路径
如:
$a = '/a/b/c/d/e.php';
$b = '/a/b/12/34/c.php';
计算出 $b 相对于 $a 的相对路径应该是 ../../c/d将()添上
/** by www.jb51.net */
$a = '/a/b/c/d/e.php';
$b = '/a/b/13/34/c.php';
echo getRelativePath($a, $b); //"../../12/34/"
function getRelativePath($a,$b){
$a2array = explode('/', $a);
$b2array = explode('/', $b);
$relativePath = '';
for( $i = 1; $i <= count($b2array)-2; $i++ ) {
$relativePath .= $a2array[$i] == $b2array[$i] ? '../' : $b2array[$i].'/';
}
return $relativePath;
}
?>
3. 写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名
例如: //www.jb51.net/abc/de/fg.php?id=1 需要取出 php 或 .php
/** by www.jb51.net */
$url = "//www.jb51.net/abc/de/fg.php?id=1";
$path = parse_url($url);
echo pathinfo($path['path'],PATHINFO_EXTENSION); //php
?>
4. 写一个函数,能够遍历一个文件夹下的所有文件和子文件夹。
答:这个我之前就在博客中写过(PHP文件遍历及文件拷贝),只是实现的方法很多,效率不一定最高
/*
*@blog //www.jb51.net
*/
function listDir($dir = '.'){
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if($file == '.' || $file == '..'){
continue;
}
if(is_dir($sub_dir = realpath($dir.'/'.$file))){
echo 'FILE in PATH:'.$dir.':'.$file.'
';
listDir($sub_dir);
}else{
echo 'FILE:'.$file.'
';
}
}
closedir($handle);
}
}
listDir('e:\www\abc');