- 在 HTML 語言中,頁面頭部的 meta 標記可以用來輸出文件的編碼格式,以下是一個標準的 meta 語句
<META http-equiv='Content-Type' content='text/html; charset=gbk'>
請使用 PHP 語言寫一個函數,把一個標準 HTML 頁面中的類似 meta 標記中的 charset 部分值改爲 big5
請注意:
(1) 需要處理完整的 html 頁面,即不光此 meta 語句
(2) 忽略大小寫
(3) ’ 和 ” 在此處是可以互換的
(4) ‘Content-Type’ 兩側的引號是可以忽略的,但 ‘text/html; charset=gbk’ 兩側的不行
(5) 注意處理多餘空格
<?php
/** http://www.phpddt.com */
$html = "<meta http-equiv='Content-Type' content='text/html; charset=gbk'>";
//匹配標準的meta標籤
$pattern = "/<meta\s+http-equiv=(\'|\")?Content-Type(\'|\")?\s+content=(\'|\")text\/html;\s+charset=(.*)(\'|\")>/i";
$replacement = "<meta http-equiv='Content-Type' content='text/html; charset=big5'>";
$result = preg_replace($pattern, $replacement, $html);
echo htmlspecialchars($result);
?>
寫一個函數,算出兩個文件的相對路徑
如$a = '/a/b/c/d/e.php';
$b = '/a/b/12/34/c.php';
計算出$b
相對於$a
的相對路徑應該是../../c/d
將()添上<?php /** by www.phpddt.com */ $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; } ?>
寫一個函數,儘可能高效的,從一個標準 url 裏取出文件的擴展名
例如:http://www.phpddt.com/abc/de/fg.php?id=1
需要取出 php 或 .php<?php /** by www.phpddt.com */ $url = "http://www.phpddt.com/abc/de/fg.php?id=1"; $path = parse_url($url); echo pathinfo($path['path'],PATHINFO_EXTENSION); //php ?>
寫一個函數,能夠遍歷一個文件夾下的所有文件和子文件夾。
答:這個我之前就在博客中寫過(PHP文件遍歷及文件拷貝),只是實現的方法很多,效率不一定最高/* *@blog http://www.phpddt.com */ 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.'<br>'; listDir($sub_dir); }else{ echo 'FILE:'.$file.'<br>'; } } closedir($handle); } } listDir('e:\www\abc');
新浪php的筆試題
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.