PHP新浪面试题及完整答案

闲来无聊找了一套新浪PHP笔试题,在有手册的情况下完成的,欢迎拍砖,砖不够我给你搬。。。真的,新手求指教,欢迎留言!

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

答:我是直接用PHP内置函数搞定的,不重复造轮子,估计出题者也是想考察基础知识,主要是解析url和一个返回文件信息的函数(扩展:取得文件后缀名的多种方法):

 

  1. <?php
  2. /** by www.phpddt.com */
  3. $url = "http://www.phpddt.com/abc/de/fg.php?id=1";
  4. $path = parse_url($url);
  5. echo pathinfo($path['path'],PATHINFO_EXTENSION); //php
  6. ?>

 

2. 在 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正则详解)忘记差不多了,弄了半天。

 

  1. <?php
  2. /** http://www.phpddt.com */
  3. $html = "<meta http-equiv='Content-Type' content='text/html; charset=gbk'>";
  4. //匹配标准的meta标签
  5. $pattern = "/<meta\s+http-equiv=(\'|\")?Content-Type(\'|\")?\s+content=(\'|\")text\/html;\s+charset=(.*)(\'|\")>/i";
  6. $replacement = "<meta http-equiv='Content-Type' content='text/html; charset=big5'>";
  7. $result = preg_replace($pattern, $replacement, $html);
  8. echo htmlspecialchars($result);
  9. ?>

 


3. 写一个函数,算出两个文件的相对路径
  如 $a = '/a/b/c/d/e.php';
  $b = '/a/b/12/34/c.php';
  计算出 $b 相对于 $a 的相对路径应该是 ../../c/d将()添上

答案:

 

  1. <?php
  2. /** by www.phpddt.com */
  3. $a = '/a/b/c/d/e.php';
  4. $b = '/a/b/13/34/c.php';
  5. echo getRelativePath($a, $b); //"../../12/34/"
  6. function getRelativePath($a,$b){
  7. $a2array = explode('/', $a);
  8. $b2array = explode('/', $b);
  9. $relativePath = '';
  10. for( $i = 1; $i <= count($b2array)-2; $i++ ) {
  11. $relativePath .= $a2array[$i] == $b2array[$i] ? '../' : $b2array[$i].'/';
  12. }
  13. return $relativePath;
  14. }
  15. ?>

 

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

答:这个我之前就在博客中写过(PHP文件遍历及文件拷贝),只是实现的方法很多,效率不一定最高

 

  1. /*
  2. *@blog http://www.phpddt.com
  3. */
  4. function listDir($dir = '.'){
  5. if ($handle = opendir($dir)) {
  6. while (false !== ($file = readdir($handle))) {
  7. if($file == '.' || $file == '..'){
  8. continue;
  9. }
  10. if(is_dir($sub_dir = realpath($dir.'/'.$file))){
  11. echo 'FILE in PATH:'.$dir.':'.$file.'<br>';
  12. listDir($sub_dir);
  13. }else{
  14. echo 'FILE:'.$file.'<br>';
  15. }
  16. }
  17. closedir($handle);
  18. }
  19. }
  20.  
  21. listDir('e:\www\abc');

 

5.简述论坛中无限分类的实现原理。

答:无限极分类,那么应该是考察递归函数吧!

第一步:建立测试数据库:

 

  1. CREATE TABLE `category` (
  2. `id` smallint(5) unsigned NOT NULL auto_increment,
  3. `fid` smallint(5) unsigned NOT NULL default '0',
  4. `value` varchar(50) NOT NULL default '',
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

第二步:插入测试数据:

 

  1. INSERT INTO `category` ( `fid`, `value`) VALUES
  2. (0, 'PHP点点通博客PHPDDT.COM'),
  3. (1,'a'),
  4. (1,'b'),
  5. (2,'c'),
  6. (2,'d'),
  7. (4,'e')

 

第三步:递归输出分类:

 

  1. <?php
  2. /** by www.phpddt.com */
  3. $conn = mysql_connect("localhost", "root", "mckee");
  4. mysql_select_db("test",$conn);
  5. mysql_query("set names utf8");
  6. $sql = "SELECT * FROM category";
  7. $res = mysql_query($sql);
  8. while($row = mysql_fetch_assoc($res)){
  9. $arr[] = array($row[id],$row[fid],$row[value]);
  10. }
  11. getCate(0);
  12. function getCate($fid = 0) {
  13. global $arr;
  14. for ($i = 0; $i < count($arr); $i++) {
  15. if ($arr[$i][1] == $fid) {
  16. echo $arr[$i][2] . "<br>";
  17. getCate($arr[$i][0]); //递归
  18. }
  19. }
  20. }
  21. ?>

 


6.设计一个网页,使得打开它时弹出一个全屏的窗口,该窗口中有一个文本框和一个按钮。用户在文本框中输入信息后点击按钮就可以把窗口关闭,而输入的信息却在主网页中显示!

 答案:尼玛。都没明白出这题目是干嘛的,新浪工程师脑子进水了吗?考察js的window对象?亲们告诉我?

index.html

 

  1. <html>
  2. <head>
  3. <title>by www.phpddt.com</title>
  4. </head>
  5. <body>
  6. <h1></h1>
  7. <script type="text/javascript">
  8. open('fullwin.html');
  9. </script>
  10. </body>
  11. </html>

 

fullwin.html

 

  1. <html>
  2. <head>
  3. <title>by www.phpddt.com</title>
  4. </head>
  5. <body>
  6. <script type="text/javascript">
  7. window.moveTo(0, 0);
  8. window.resizeTo(window.screen.width, window.screen.height);
  9. var s = prompt('请输入:');
  10. window.opener.document.getElementsByTagName('h1')[0].innerText = s;
  11. window.close();
  12. </script>
  13. </body>
  14. </html>

 

PS:这只是sina PHP笔试题中的笔试题,还有几十道选择和填空题,在短时间内完成还是有一定工作量的,反正我是不行,你呢?

posted @ 2015-11-30 09:51  Sun丶老板  阅读(345)  评论(0编辑  收藏  举报