編程題:
1. 寫一個(gè)函數(shù)舵稠,盡可能高效的,從一個(gè)標(biāo)準(zhǔn) url 里取出文件的擴(kuò)展名
例如:http://www.sina.com.cn/abc/de/fg.PHP?id=1 需要取出 php 或 .php
答案1:
function getExt($url){
$arr = parse_url($url);
$file = basename($arr['path']);
$ext = explode(".",$file);
return $ext[1];
}
答案2:
function getExt($url) {
$url = basename($url);
$pos1 = strpos($url,".");
$pos2 = strpos($url,"?");
if(strstr($url,"?")){
return substr($url,$pos1 + 1,$pos2 -$pos1 - 1);
} else {
return substr($url,$pos1);
}
}
2. 在 HTML 語言中辉川,頁面頭部的 meta標(biāo)記可以用來輸出文件的編碼格式叶沛,以下是一個(gè)標(biāo)準(zhǔn)的 meta 語句
請(qǐng)使用 PHP 語言寫一個(gè)函數(shù)惰赋,把一個(gè)標(biāo)準(zhǔn) HTML 頁面中的類似 meta 標(biāo)記中的 charset 部分值改為 big5
請(qǐng)注意:
1. 需要處理完整的 html 頁面,即不光此 meta 語句
2. 忽略大小寫
3. ' 和 " 在此處是可以互換的
4. 'Content-Type' 兩側(cè)的引號(hào)是可以忽略的蕴侧,但 'text/html; charset=gbk' 兩側(cè)的不行
5. 注意處理多余空格
$str=File_get_contents(‘xxx.php’);
Preg_replace(‘//’,‘//’,$str)
3. 寫一個(gè)函數(shù)择同,算出兩個(gè)文件的相對(duì)路徑
如 $a ='/a/b/c/d/e.php';
$b ='/a/b/12/34/c.php';
計(jì)算出 $b 相對(duì)于 $a 的相對(duì)路徑應(yīng)該是 ../../c/d將()添上
答:function getRelativePath($a, $b) {
$returnPath = array(dirname($b));
$arrA = explode('/', $a);
$arrB = explode('/', $returnPath[0]);
for ($n = 1, $len = count($arrB); $n <$len; $n++) {
if ($arrA[$n] != $arrB[$n]) {
break;
}
}
if ($len - $n > 0) {
$returnPath = array_merge($returnPath,array_fill(1, $len - $n, '..'));
}
$returnPath = array_merge($returnPath,array_slice($arrA, $n));
return implode('/', $returnPath);
}
echo getRelativePath($a, $b);
填空題:
1.在PHP中,當(dāng)前腳本的名稱(不包括路徑和查詢字符串)記錄在預(yù)定義變量__$_SERVER['PHP_SELF']__中;而鏈接到當(dāng)前頁面的URL記錄在預(yù)定義變量__$_SERVER['HTTP_REFERER']__
中
2.執(zhí)行程序段將輸出__0__净宵。
3.在HTTP 1.0中敲才,狀態(tài)碼 401 的含義是__未被授權(quán)__;如果返回“找不到文件”的提示裹纳,則可用 header 函數(shù),其語句為__header(‘location:xxx.php’)__紧武。
4.數(shù)組函數(shù) arsort 的作用是__對(duì)數(shù)組進(jìn)行逆向排序并保持索引關(guān)系__;語句error_reporting(2047)的作用是__報(bào)告所有錯(cuò)誤和警告__剃氧。
5.PEAR中的數(shù)據(jù)庫連接字符串格式是__。
6.寫出一個(gè)正則表達(dá)式阻星,過慮網(wǎng)頁上的所有JS/VBS腳本(即把scrīpt標(biāo)記及其內(nèi)容都去掉):preg_replace("/].*?>.*?/si","newinfo", $script);
7.以Apache模塊的方式安裝PHP朋鞍,在文件http.conf中首先要用語句____動(dòng)態(tài)裝載PHP模塊,然后再用語句____使得Apache把所有擴(kuò)展名為php的文件都作為PHP腳本處理妥箕。
LoadModule php5_module "c:/php/php5apache2.dll" ,
AddTypeapplication/x-httpd-php .php,
8.語句 include 和 require 都能把另外一個(gè)文件包含到當(dāng)前文件中滥酥,它們的區(qū)別是____;為了避免多次包含同一文件,可以用語句__require_once||include_once__來代替它們矾踱。
9.類的屬性可以序列化后保存到 session 中恨狈,從而以后可以恢復(fù)整個(gè)類疏哗,這要用到的函數(shù)是__unserialize__呛讲。
10.一個(gè)函數(shù)的參數(shù)不能是對(duì)變量的引用,除非在php.ini中把__allow_call_time_pass_reference boolean__設(shè)為on.
11.SQL中LEFT JOIN的含義是__自然左外鏈接__返奉。如果 tbl_user記錄了學(xué)生的姓名(name)和學(xué)號(hào)(ID)贝搁,tbl_score記錄了學(xué)生(有的學(xué)生考試以后被開除了,沒有其記錄)的學(xué)號(hào)(ID)
和考試成績(score)以及考試科目(subject)芽偏,要想打印出各個(gè)學(xué)生姓名及對(duì)應(yīng)的的各科總成績雷逆,則可以用SQL語句__select? *? fromtbl_user left jion tbl_score on tbl_user.id=tbl_score.uid__。
12.在PHP中污尉,heredoc是一種特殊的字符串膀哲,它的結(jié)束標(biāo)志必須____。
<<
Sdashkdhklahdklh
EOT
編程題:
13.寫一個(gè)函數(shù)被碗,能夠遍歷一個(gè)文件夾下的所有文件和子文件夾某宪。
答:
function my_scandir($dir)
{
$files = array();
if ( $handle = opendir($dir) ) {
while ( ($file = readdir($handle)) !==false ) {
$file=$dir.’/’.$file
if ( $file != ".."&& $file != "." ) {
if ( is_dir($dir ."/" . $file) ) {
$files[$file] =scandir($dir . "/" . $file);
}else {
$files[] = $file;
}
}
}
closedir($handle);
return $files;
}
}
14.簡述論壇中無限分類的實(shí)現(xiàn)原理。
答:
/*
數(shù)據(jù)表結(jié)構(gòu)如下:
CREATE TABLE `category` (
`categoryID` smallint(5) unsigned NOT NULLauto_increment,
`categoryParentID` smallint(5) unsigned NOTNULL default '0',
`categoryName` varchar(50) NOT NULL default'',
PRIMARY KEY (`categoryID`)
) ENGINE=MyISAM DEFAULTCHARSET=gbk;
INSERT INTO `category` (`categoryParentID`, `categoryName`) VALUES
(0, '一級(jí)類別'),
(1, '二級(jí)類別'),
(1, '二級(jí)類別'),
(1, '二級(jí)類別'),
(2, '三級(jí)類別'),
(2, '333332'),
(2, '234234'),
(3, 'aqqqqqd'),
(4, '哈哈'),
(5, '66333666');
*/
//指定分類id變量$category_id,然后返回該分類的所有子類
//$default_category為默認(rèn)的選中的分類
functionGet_Category($category_id = 0,$level = 0, $default_category = 0)
{
global $DB;
$sql = "SELECT * FROM category ORDER BYcategoryID DESC";
$result = $DB->query( $sql );
while ($rows = $DB->fetch_array($result))
{
$category_array[$rows[categoryParentID]][$rows[categoryID]]= array('id' => $rows[categoryID], 'parent' => $rows[categoryParentID],'name' => $rows
[categoryName]);
}
if (!isset($category_array[$category_id]))
{
return "";
}
foreach($category_array[$category_id] AS $key=> $category)
{
if ($category['id'] == $default_category)
{
echo "
}else
{
echo "
}
if ($level > 0)
{
echo ">" . str_repeat( "", $level ) . " " . $category['name'] ."\n";
}
else
{
echo ">" . $category['name'] ."\n";
}
Get_Category($key, $level + 1,$default_category);
}
unset($category_array[$category_id]);
}
/*
函數(shù)返回的數(shù)組格式如下所示:
Array
(
[1] => Array ( [id] => 1 [name] =>一級(jí)類別[level] => 0 [ParentID] => 0 )
[4] => Array ( [id] => 4 [name] =>二級(jí)類別[level] => 1 [ParentID] => 1 )
[9] => Array ( [id] => 9 [name] =>哈哈[level] => 2 [ParentID] => 4 )
[3] => Array ( [id] => 3 [name] =>二級(jí)類別[level] => 1 [ParentID] => 1 )
[8] => Array ( [id] => 8 [name] =>aqqqqqd [level] => 2 [ParentID] => 3 )
[2] => Array ( [id] => 2 [name] =>二級(jí)類別[level] => 1 [ParentID] => 1 )
[7] => Array ( [id] => 7 [name] =>234234 [level] => 2 [ParentID] => 2 )
[6] => Array ( [id] => 6 [name] =>333332 [level] => 2 [ParentID] => 2 )
[5] => Array ( [id] => 5 [name] =>三級(jí)類別[level] => 2 [ParentID] => 2 )
[10] => Array ( [id] => 10 [name] =>66333666 [level] => 3 [ParentID] => 5 )
)
*/
//指定分類id,然后返回?cái)?shù)組
functionCategory_array($category_id = 0,$level=0)
{
global $DB;
$sql = "SELECT * FROM category ORDER BYcategoryID DESC";
$result = $DB->query($sql);
while ($rows = $DB->fetch_array($result))
{
$category_array[$rows['categoryParentID']][$rows['categoryID']]= $rows;
}
foreach ($category_array AS $key=>$val)
{
if ($key == $category_id)
{
foreach ($val AS $k=> $v)
{
$options[$k] =
array(
'id' => $v['categoryID'], 'name' =>$v['categoryName'], 'level' => $level, 'ParentID'=>$v['categoryParentID']
);
$children = Category_array($k, $level+1);
if (count($children) > 0)
{
$options = $options + $children;
}
}
}
}
unset($category_array[$category_id]);
return $options;
}
?>
class cate
{
function Get_Category($category_id =0,$level = 0, $default_category = 0)
{
echo $category_id;
$arr = array(
'0' => array(
'1' =>array('id' => 1, 'parent' => 0, 'name' => '1111'),
'2' =>array('id' => 2, 'parent' => 0, 'name' => '2222'),
'4' =>array('id' => 4, 'parent' => 0, 'name' => '4444')
),
'1' => array(
'3' =>array('id' => 3, 'parent' => 1, 'name' => '333333'),
'5' =>array('id' => 5, 'parent' => 1, 'name' => '555555')
),
'3' => array(
'6' =>array('id' => 6, 'parent' => 3, 'name' => '66666'),
'7' =>array('id' => 7, 'parent' => 3, 'name' => '77777')
),
'4' => array(
'8' =>array('id' => 8, 'parent' => 4, 'name' => '8888'),
'9' =>array('id' => 9, 'parent' => 4, 'name' => '9999')
)
);
if (!isset($arr[$category_id]))
{
return "";
}
foreach($arr[$category_id] AS $key=> $cate)
{
if ($cate['id'] ==$default_category)
{
$txt = "
}else{
$txt = "
}
if ($level > 0)
{
$txt1 = ">" .str_repeat( "-", $level ) . " " . $cate['name'] ."\n";
}else{
$txt1 = ">" .$cate['name'] . "\n";
}
$val = $txt.$txt1;
echo $val;
self::Get_Category($key,$level + 1, $default_category);
}
}
function getFlush($category_id =0,$level = 0, $default_category = 0)
{
ob_start();
self::Get_Category($category_id,$level, $default_category);
$out = ob_get_contents();
ob_end_clean();
return $out;
}
}
$id =$_GET['id'];
echo"";
$c = new cate();
//$c->Get_Category();
$ttt=? $c->getFlush($id,'0','3');
echo $ttt;
echo"";
?>
著作權(quán)歸作者所有锐朴。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán)兴喂,非商業(yè)轉(zhuǎn)載請(qǐng)注明出處》僦荆互聯(lián)網(wǎng)+時(shí)代衣迷,時(shí)刻要保持學(xué)習(xí),攜手千鋒PHP,Dream?It?Possible酱酬。