您的位置:首页 > 职场人生

PHP编程面试题

2008-10-03 13:10 337 查看
一道PHP编程面试题

写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名
例如: http://www.sina.com.cn/abc/de/fg.php?id=1 需要取出 php 或 .php

<?php
function getUrlExt($str) {
$a = explode(”/”,$str);
$b = explode(”.”,$a[count($a)-1]);
$c = $b[count($b)-1];
$d = explode(”?”,$c);
return $d[0];
}
echo getUrlExt(”http://www.sina.com.cn/abc/de/fg.php”);
?>

function abc(&string){
$a = explode('?',$x);
$a = array_reverse(explode('.',basename($a[0])));
return $a[0];
}

$x ='http://www.sina.com.cn/abc/de/fg.php?id=1';
echo abc($x)

2. 写一个函数,算出两个文件的相对路径
如 $a = ’/a/b/c/d/e.php’;
$b = ’/a/b/12/34/c.php’;

计算出 $b 相对于 $a 的相对路径应该是 ../../c/d

function relative_dir($target, $base) {
$a_ = explode(”/”,$target);
$b_ = explode(”/”,$base);
$j = 0;

$count = (count($a_)>count($b_))?count($b_):count($a_);

$r_a = array();
$r_b = array();

for ($i=0;$a_[0] == $b_[0];$i++) {

if ($a_[$i] == $b_[$i]) {
array_shift($a_);
array_shift($b_);
continue;
}
if ($i<count($a_)) $r_a[$j] = $a_[$i];
if ($i<count($b_)) $r_b[$j] = $b_[$i];
$j++;
}
$count = count($r_b);
$new = “”;
for ($i=0;$i <= $count-1;$i++) {
$new .= “../”;
}
return $new.implode(”/”,$r_a);
}

function abc(&$string1,&$string2){
$x = explode('/',substr($a,1));
$y = explode('/',substr($b,1));
$z = array_intersect(&$x,&$y);
foreach ($z as $key => $val){
$x[$key] = '..';
}
return implode('/',$x);$a = '/a/b/c/d/e.php';
$b = '/a/b/12/34/c.php';

echo abc($a,$b)

写一个函数,能够遍历一个文件夹下的所有文件和子文件夹。

<?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;
}
}
}
print_r(my_scandir("D:Program FilesInternet ExplorerMUI"));
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: