2014新浪php面试题

分类:php

这是一个朋友在新浪面试之后把所有面试题与答案都拿出来了,下面我把东西记录一下,有用得上的兄弟们可先看一次,对过去面试有个底。

 

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

 

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

 

 

<?php

/** by  */

$url = “/abc/de/fg.php?id=1”;

$path = parse_url($url);

echo pathinfo($path['path'],PATHINFO_EXTENSION);  //php

?>

 

 

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正则详解)忘记差不多了,弄了半天,代码如下:

 

 

  <?php

/**  */

$html = ”<meta http-equiv='Content-Type' content='text/html; charset=gbk'>“;

//匹配标准的meta标签

$pattern = ”/<metas+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);

?>

 

 

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

答案:代码如下:

 

 

  <?php

/** by  */

$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;

}

?>

 

 

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

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

 

 

  /*

*@blog

*/

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:wwwabc’);

 

 

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

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

第一步:建立测试数据库,代码如下:

 

 

  CREATE TABLE `category` (

`id` smallint(5) unsigned NOT NULL auto_increment,

`fid` smallint(5) unsigned NOT NULL default '0',

`value` varchar(50) NOT NULL default '',

PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

 

第二步:插入测试数据,代码如下:

 

 

INSERT INTO `category` ( `fid`, `value`) VALUES

(0, 'PHP点点通博客Pxx.COM‘),

(1,'a’),

(1,'b‘),

(2,'c’),

(2,'d‘),

(4,'e’)

 

 

第三步:递归输出分类,代码如下:

 

 

<?php

/** by  */

$conn = mysql_connect(”localhost“, ”root“, ”mckee“);

mysql_select_db(”test“,$conn);

mysql_query(”set names utf8“);

$sql = ”SELECT * FROM category“;

$res = mysql_query($sql);

while($row = mysql_fetch_assoc($res)){

$arr[] = array($row[id],$row[fid],$row[value]);

}

getCate(0);

function getCate($fid = 0) {

global $arr;

for ($i = 0; $i < count($arr); $i++) {

if ($arr[$i][1] == $fid) {

echo $arr[$i][2] . ”<br>“;

getCate($arr[$i][0]); //递归

}

}

}

?>

 

 

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

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

index.html,代码如下:

 

 

<html>

<head>

<title>by </title>

</head>

<body>

<h1></h1>

<script type=”text/javascript“>

open(‘fullwin.html’);

</script>

</body>

</html>

 

 

fullwin.html,代码如下:

 

 

<html>

<head>

<title>by </title>

</head>

<body>

<script type=”text/javascript“>

window.moveTo(0, 0);

window.resizeTo(window.screen.width, window.screen.height);

var s = prompt(‘请输入:’);

window.opener.document.getElementsByTagName(‘h1’)[0].innerText = s;

window.close();

</script>

</body>

</html>

 

 

unset引用,代码如下:

 

 

<?php

$a = ”this is a php blog“;

$b = & $a;

unset($b);

echo $a; //this is a php blog

?>

 

 

这个我很清楚,unset($b),只是断开了变量名和值得绑定,但是神奇的是如下代码:

 

 

<?php

$a = ”test“;

$b = & $a;

unset($a);

echo $b;//test

?>

 

 

但是我销毁$a了,为嘛$b的值还在呢?晕了,求解!感谢下面网友回复,让我明白了。

关于unset()函数使用注意:只有当指向该值的所有变量(比如有引用变量指向该值)都被销毁后,地址才会被释放,如下:

 

 

  <?php

/** by www.phpthink.cn */

$a = ”test“;

$b = & $a;

unset($a);

unset($b);

echo $b;//输出空

?>

 

 

本文链接: 2014新浪php面试题
版权所有:布士收藏夹在网络收集,如有侵犯你的权利, 请联系QQ:641868752。

评论

发表评论

*

* (显示gravatar头像)

Ctrl+Enter快捷回复

© 2014 布士收藏夹 all rights reserved.