新浪php的笔试题

  1. 在 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);
    ?>
  2. 写一个函数,算出两个文件的相对路径
    $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;
                    }
                ?>
  3. 写一个函数,尽可能高效的,从一个标准 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
        ?>
  4. 写一个函数,能够遍历一个文件夹下的所有文件和子文件夹。
    答:这个我之前就在博客中写过(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');
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值