六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 491|回复: 0

PHP面试题编程-IT论坛-it社区-it星球论坛-php开发

[复制链接]
 楼主| 发表于 2014-3-17 11:56:02 | 显示全部楼层 |阅读模式
PHP面试题编程-IT论坛-it社区-it星球论坛-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(”
?>  
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("Drogram FilesInternet ExplorerMUI"));
?>
PHP面试题编程-IT论坛-it社区-it星球论坛-php开发
摘自:http://hi.baidu.com/dinenghou/item/f265118a92017ee9e496e0b9

该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表