课程咨询 :13623629309

太原PHP培训 > 雇主企业 > 企业面试题 > PHP_面试笔试题--编程题(二)
  • PHP_面试笔试题--编程题(二)

    发布:达内      来源:太原达内PHP学院      时间:2015-08-27

    • 编程题
      1. 写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名
      例如: http://www.sina.com.cn/abc/de/fg.php?id=1 需要取出 php 或 .php
            function getext($url) {
             $w_param = pathinfo($url);
            $str = $w_param['filename'];
            list($type, $vars) = explode('?',$str);
            $kuozhan=explode('.',$type);
             return $kuozhan[1];
      }


      2. 在 HTML 语言中,页面头部的 meta 标记可以用来输出文件的编码格式,以下是一个标准的 meta 语句
      请使用 PHP 语言写一个函数,把一个标准 HTML 页面中的类似 meta 标记中的 charset 部分值改为 big5
      请注意:
      1. 需要处理完整的 html 页面,即不光此 meta 语句
      2. 忽略大小写
      3. ' 和 " 在此处是可以互换的
      4. 'Content-Type' 两侧的引号是可以忽略的,但 'text/html; charset=gbk' 两侧的不行
      5. 注意处理多余空格


      3.写一个函数,算出两个文件的相对路径
      如 $a = '/a/b/c/d/e.php';
      $b = '/a/b/12/34/c.php';
      计算出 $b 相对于 $a 的相对路径应该是 ../../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);



      4.写一个函数,能够遍历一个文件夹下的所有文件和子文件夹。
        function my_scandir($dir)
      {
           $files = array();
           if ( $handle = opendir($dir) ) {
               while ( ($file = readdir($handle)) !== false ) {
                   if ( $file != ".." && $file != "." ) {
                       if ( is_dir($dir . "/" . $file) ) {
                           $files[$file] = scandir($dir . "/" . $file);
                       }else {
                           $files[] = $file;
                       }
                   }
               }
               closedir($handle);
               return $files;
           }
      }

      5.简述论坛中无限分类的实现原理。
       答:
      <?php
      /*
      数据表结构如下:
      CREATE TABLE `category` (
      `categoryID` smallint(5) unsigned NOT NULL auto_increment,
      `categoryParentID` smallint(5) unsigned NOT NULL default '0',
      `categoryName` varchar(50) NOT NULL default '',
      PRIMARY KEY (`categoryID`)
      ) ENGINE=MyISAM DEFAULT CHARSET=gbk;
      INSERT INTO `category` ( `categoryParentID`, `categoryName`) VALUES
      (0, '一级类别'),
      (1, '二级类别'),
      (1, '二级类别'),
      (1, '二级类别'),
      (2, '三级类别'),
      (2, '333332'),
      (2, '234234'),
      (3, 'aqqqqqd'),
      (4, '哈哈'),
      (5, '66333666');
      */
      //指定分类id变量$category_id,然后返回该分类的所有子类
      //$default_category为默认的选中的分类
      function Get_Category($category_id = 0,$level = 0, $default_category =0)
      {
      global $DB;
      $sql = "SELECT * FROM category ORDER BY categoryID 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 "<option selectedvalue=".$category['id']."";
      }else
      {
      echo "<option value=".$category['id']."";
      }
      if ($level > 0)
      {
      echo ">" . str_repeat( " ", $level ) . "" . $category['name'] . "</option>/n";
      }
      else
      {
      echo ">" . $category['name'] ."</option>/n";
      }
      Get_Category($key, $level + 1, $default_category);
      }
      unset($category_array[$category_id]);
      }
      /*
      函数返回的数组格式如下所示:
      Array
      (
      [1] => Array ( [id] => 1 [name] => 一级类别[level] => 0 [ParentID] => 0 )
      [4] => Array ( [id] => 4 [name] => 二级类别[level] => 1 [ParentID] => 1 )
      [9] => Array ( [id] => 9 [name] => 哈哈[level] => 2 [ParentID] => 4 )
      [3] => Array ( [id] => 3 [name] => 二级类别[level] => 1 [ParentID] => 1 )
      [8] => Array ( [id] => 8 [name] => aqqqqqd [level] => 2[ParentID] => 3 )
      [2] => Array ( [id] => 2 [name] => 二级类别[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] => 三级类别[level] => 2 [ParentID] => 2 )
      [10] => Array ( [id] => 10 [name] => 66333666 [level] => 3[ParentID] => 5 )
      )
      */
      //指定分类id,然后返回数组
      function Category_array($category_id = 0,$level=0)
      {
      global $DB;
      $sql = "SELECT * FROM category ORDER BY categoryID 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;
      }
      ?>

      <?php
      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 = "<option selected value=".$cate['id']."";
                     }else{
                         $txt = "<option value=".$cate['id']."";
                     }
                 
                     if ($level > 0)
                     {
                        $txt1 = ">" . str_repeat( "-", $level ) . " ". $cate['name'] . "</option>/n";
                     }else{
                         $txt1 = ">" . $cate['name'] . "</option>/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 "<select>";
      $c = new cate();
      //$c->Get_Category();
      $ttt=  $c->getFlush($id,'0','3');
      echo $ttt;
      echo "</select>";
      ?>

上一篇:腾讯招聘PHP工程师面试笔试题及答案

下一篇:PHP_面试笔试题--填空题

最新开班日期  |  更多

php高级开发名企定制班(剩2个名额)

php高级开发名企定制班(剩2个名额)

开班日期:12-30

php高级开发周末班(剩5个名额)

php高级开发周末班(剩5个名额)

开班日期:12-30

php高级开发免费试听(剩5个名额)

php高级开发免费试听(剩5个名额)

开班日期:12-30

更多高级开发工程师精品班

更多高级开发工程师精品班

开班日期:12-30

  • 地址:山西省太原市小店区学府街长治路高新国际A座24层
  • 课程培训电话:13623629309     全国服务监督电话:400-827-0010
  • 服务邮箱 ts@tedu.cn
  • 2001-2016 达内国际公司(TARENA INTERNATIONAL,INC.) 版权所有 京ICP证08000853号-56

    在线客服系统