PHP编程面试题

 一道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"));
?>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值