范文资料网>人事资料>招聘与面试>《新浪php面试题答案

新浪php面试题答案

时间:2022-04-19 10:19:51 招聘与面试 我要投稿
  • 相关推荐

新浪php面试题答案

1. 写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名

新浪php面试题答案

例如: /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点点通博客http://www.ahsrst.cn'),

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

代码如下 复制代码

<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面试题答案2015-11-01 13:54 | #2楼

1. 写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名

例如: http://www.ahsrst.cn需要取出 php 或 .php

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

<?php /** by http://www.ahsrst.cn */ $url = "http://www.ahsrst.cn"; $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 /** http://www.ahsrst.cn */ $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);?>

3. 写一个函数,算出两个文件的相对路径

如 $a = '/a/b/c/d/e.php';

$b = '/a/b/12/34/c.php';

计算出 $b 相对于 $a 的相对路径应该是 ../../c/d将()添上

答案:

<?php /** by http://www.ahsrst.cn */ $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 http://www.ahsrst.cn */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');

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点点通博客http://www.ahsrst.cn'), (1,'a'), (1,'b'), (2,'c'), (2,'d'), (4,'e')

第三步:递归输出分类:

<?php/** by http://www.ahsrst.cn */$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 http://www.ahsrst.cn</title> </head><body><h1></h1><script type="text/javascript">open('fullwin.html');</script></body></html>

fullwin.html

<html> <head> <title>by http://www.ahsrst.cn</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>

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

【新浪php面试题答案】相关文章:

应急面试题目及答案06-06

情景面试题题目及答案03-15

营销招聘面试题及答案06-01

物业电工面试题附答案03-15

金融业面试题目及答案03-09

主持人面试题目及答案04-19

常见医院面试题目与参考答案05-01

项目经理面试题目及其答案03-17

招警考试经典面试题及参考答案03-06

医院会计面试题及答案06-05