闲来无聊找了一套新浪PHP笔试题,在有手册的情况下完成的,欢迎拍砖,砖不够我给你搬。。。真的,新手求指教,欢迎留言!
1. 写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名
例如: http://www.phpddt.com/abc/de/fg.php?id=1 需要取出 php 或 .php
答:我是直接用PHP内置函数搞定的,不重复造轮子,估计出题者也是想考察基础知识,主要是解析url和一个返回文件信息的函数(扩展:取得文件后缀名的多种方法):
- <?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
- ?>
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.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);
- ?>
3. 写一个函数,算出两个文件的相对路径
如 $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;
- }
- ?>
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');
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点点通博客PHPDDT.COM'),
- (1,'a'),
- (1,'b'),
- (2,'c'),
- (2,'d'),
- (4,'e')
第三步:递归输出分类:
- <?php
- /** by www.phpddt.com */
- $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
fullwin.html
PS:这只是sina PHP笔试题中的笔试题,还有几十道选择和填空题,在短时间内完成还是有一定工作量的,反正我是不行,你呢?