php基础题的答案

(1)写一个函数获取URL的文件后缀,例如“http://www.feiyan.info/test.php?c=class&m=method”(获得php或者.php)

[php]  view plain copy
  1. <?php  
  2.   
  3. /** 
  4.  * 给定url,获取文件后缀 
  5.  * @param string $url 
  6.  * @return string 
  7.  */  
  8. function getUrlPostfix ($url)  
  9. {  
  10.     $url_arr = explode('.'$url);  
  11.     $postfix = $url_arr[count($url_arr) - 1];  
  12.       
  13.     $substr = substr($postfix, 0, 3);  
  14.     return $substr;  
  15. }  
  16.   
  17. $url = "http://www.feiyan.info/test.php?c=class&m=method";  
  18. $str = getUrlPostfix($url);  
  19. echo $str . "\n";  


(2)写一个函数,将一个字符串每隔三个字符添加一个逗号,例如把字符串1234567890转换为1,234,567,890(金融中用到的记账方法)

[php]  view plain copy
  1. <?php  
  2.   
  3. /** 
  4.  * 每隔3个字符,用逗号进行分隔 
  5.  * @param string $str 
  6.  * @return string 
  7.  */  
  8. function splitStrWithComma ($str)  
  9. {  
  10.     $arr = array();  
  11.     $len = strlen($str);  
  12.       
  13.     for ($i = $len - 1; $i >= 0;) {  
  14.         $new_str = "";  
  15.         for ($j = $i$j > $i - 3 && $j >= 0; $j --) {  
  16.             $new_str .= $str[$j];  
  17.         }  
  18.         $arr[] = $new_str;  
  19.         $i = $j;  
  20.     }  
  21.       
  22.     $string = implode(','$arr);  
  23.       
  24.     // 翻转字符串自己实现  
  25.     // $string = strrev($string);  
  26.     for ($i = 0, $j = strlen($string) - 1; $i <= $j$i ++, $j --) {  
  27.         $tmp = $string[$i];  
  28.         $string[$i] = $string[$j];  
  29.         $string[$j] = $tmp;  
  30.     }  
  31.       
  32.     return $string;  
  33. }  
  34.   
  35. $str = "1234567890";  
  36. $new_str = splitStrWithComma($str);  
  37. echo $new_str . "\n";  

(3)写一个php函数算出两个文件的相对路径。例如$a="/a/b/c/d/e.php"; $b="/a/b/12/34/c.php",B相对于A的相对路径是什么?

这道题目可以看成是求第一个公共节点的题目,网上流传的代码大部分是错的,考虑不周全,当然我这个也只是用“../”去表示,没用"./"

[php]  view plain copy
  1. <?php  
  2.   
  3. /** 
  4.  * 求$b相对于$a的相对路径 
  5.  * @param string $a 
  6.  * @param string $b 
  7.  * @return string 
  8.  */  
  9. function getRelativePath ($a$b)  
  10. {  
  11.     $patha = explode('/'$a);  
  12.     $pathb = explode('/'$b);  
  13.       
  14.     $counta = count($patha) - 1;  
  15.     $countb = count($pathb) - 1;  
  16.       
  17.     $path = "../";  
  18.     if ($countb > $counta) {  
  19.         while ($countb > $counta) {  
  20.             $path .= "../";  
  21.             $countb --;  
  22.         }  
  23.     }  
  24.       
  25.     // 寻找第一个公共结点  
  26.     for ($i = $countb - 1; $i >= 0;) {  
  27.         if ($patha[$i] != $pathb[$i]) {  
  28.             $path .= "../";  
  29.             $i --;  
  30.         } else { // 判断是否为真正的第一个公共结点,防止出现子目录重名情况  
  31.             for ($j = $i - 1, $flag = 1; $j >= 0; $j --) {  
  32.                 if ($patha[$j] == $pathb[$j]) {  
  33.                     continue;  
  34.                 } else {  
  35.                     $flag = 0;  
  36.                     break;  
  37.                 }  
  38.             }  
  39.               
  40.             if ($flag)  
  41.                 break;  
  42.             else  
  43.                 $i ++;  
  44.         }  
  45.     }  
  46.       
  47.     for ($i += 1; $i <= $counta$i ++) {  
  48.         $path .= $patha[$i] . "/";  
  49.     }  
  50.       
  51.     return $path;  
  52. }  
  53.   
  54. $a = "/a/c/d/e.php";  
  55. $b = "/a/c.php";  
  56.   
  57. $path = getRelativePath($a$b);  
  58. echo $path;  

(4)计算两个日期之间的天数

[php]  view plain copy
  1. <?php  
  2.   
  3. /** 
  4.  * 求两个日期之间相差的天数(针对1970年1月1日之后,求之前可以采用泰勒公式) 
  5.  * @param string $day1 
  6.  * @param string $day2 
  7.  * @return number 
  8.  */  
  9. function diffBetweenTwoDays ($day1$day2)  
  10. {  
  11.     $second1 = strtotime($day1);  
  12.     $second2 = strtotime($day2);  
  13.       
  14.     if ($second1 < $second2) {  
  15.         $tmp = $second2;  
  16.         $second2 = $second1;  
  17.         $second1 = $tmp;  
  18.     }  
  19.       
  20.     return ($second1 - $second2) / 86400;  
  21. }  
  22.   
  23. $day1 = "2013-07-27";  
  24. $day2 = "2013-08-04";  
  25.   
  26. $diff = diffBetweenTwoDays($day1$day2);  
  27. echo $diff."\n"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值