昨晚实验室一师弟在微薄上@我,给我发了几道php的基础面试题,这里把我写的答案贴出来
题目
(1)写一个函数获取url的文件后缀,例如“http://www.feiyan.info/test.php?c=class&m=method”(获得php或者.php)
[php]
/**
* 给定url,获取文件后缀
* @param string $url
* @return string
*/
function geturlpostfix ($url)
{
$url_arr = explode('.', $url);
$postfix = $url_arr[count($url_arr) - 1];
$substr = substr($postfix, 0, 3);
return $substr;
}
$url = http://www.feiyan.info/test.php?c=class&m=method;
$str = geturlpostfix($url);
echo $str . \n;
$i - 3 && $j >= 0; $j --) {
$new_str .= $str[$j];
}
$arr[] = $new_str;
$i = $j;
}
$string = implode(',', $arr);
// 翻转字符串自己实现
// $string = strrev($string);
for ($i = 0, $j = strlen($string) - 1; $i $tmp = $string[$i];
$string[$i] = $string[$j];
$string[$j] = $tmp;
}
return $string;
}
$str = 1234567890;
$new_str = splitstrwithcomma($str);
echo $new_str . \n;
= 0;) {
$new_str = ;
for ($j = $i; $j > $i - 3 && $j >= 0; $j --) {
$new_str .= $str[$j];
}
$arr[] = $new_str;
$i = $j;
}
$string = implode(',', $arr);
// 翻转字符串自己实现
// $string = strrev($string);
for ($i = 0, $j = strlen($string) - 1; $i $tmp = $string[$i];
$string[$i] = $string[$j];
$string[$j] = $tmp;
}
return $string;
}
$str = 1234567890;
$new_str = splitstrwithcomma($str);
echo $new_str . \n;
(3)写一个php函数算出两个文件的相对路径。例如$a=/a/b/c/d/e.php; $b=/a/b/12/34/c.php,b相对于a的相对路径是什么?
这道题目可以看成是求第一个公共节点的题目,网上流传的代码大部分是错的,考虑不周全,当然我这个也只是用“../”去表示,没用./
[php]
$counta) {
while ($countb > $counta) {
$path .= ../;
$countb --;
}
}
// 寻找第一个公共结点
for ($i = $countb - 1; $i >= 0;) {
if ($patha[$i] != $pathb[$i]) {
$path .= ../;
$i --;
} else { // 判断是否为真正的第一个公共结点,防止出现子目录重名情况
for ($j = $i - 1, $flag = 1; $j >= 0; $j --) {
if ($patha[$j] == $pathb[$j]) {
continue;
} else {
$flag = 0;
break;
}
}
if ($flag)
break;
else
$i ++;
}
}
for ($i += 1; $i $path .= $patha[$i] . /;
}
return $path;
}
$a = /a/c/d/e.php;
$b = /a/c.php;
$path = getrelativepath($a, $b);
echo $path;
$counta) {
while ($countb > $counta) {
$path .= ../;
$countb --;
}
}
// 寻找第一个公共结点
for ($i = $countb - 1; $i >= 0;) {
if ($patha[$i] != $pathb[$i]) {
$path .= ../;
$i --;
} else { // 判断是否为真正的第一个公共结点,防止出现子目录重名情况
for ($j = $i - 1, $flag = 1; $j >= 0; $j --) {
if ($patha[$j] == $pathb[$j]) {
continue;
} else {
$flag = 0;
break;
}
}
if ($flag)
break;
else
$i ++;
}
}
for ($i += 1; $i $path .= $patha[$i] . /;
}
return $path;
}
$a = /a/c/d/e.php;
$b = /a/c.php;
$path = getrelativepath($a, $b);
echo $path;
(4)计算两个日期之间的天数
[php]
/**
* 求两个日期之间相差的天数(针对1970年1月1日之后,求之前可以采用泰勒公式)
* @param string $day1
* @param string $day2
* @return number
*/
function diffbetweentwodays ($day1, $day2)
{
$second1 = strtotime($day1);
$second2 = strtotime($day2);
if ($second1 $tmp = $second2;
$second2 = $second1;
$second1 = $tmp;
}
return ($second1 - $second2) / 86400;
}
$day1 = 2013-07-27;
$day2 = 2013-08-04;
$diff = diffbetweentwodays($day1, $day2);
echo $diff.\n;
/**
* 求两个日期之间相差的天数(针对1970年1月1日之后,求之前可以采用泰勒公式)
* @param string $day1
* @param string $day2
* @return number
*/
function diffbetweentwodays ($day1, $day2)
{
$second1 = strtotime($day1);
$second2 = strtotime($day2);
if ($second1 $tmp = $second2;
$second2 = $second1;
$second1 = $tmp;
}
return ($second1 - $second2) / 86400;
}
$day1 = 2013-07-27;
$day2 = 2013-08-04;
$diff = diffbetweentwodays($day1, $day2);
echo $diff.\n;
