面试题02
#关于字符串的测试 //$v = '00'; // $x2 = $v+1; //echo $v&1; //echo($v == 1)?'相等':'不相等'; //结论:对于字符串的以0开头后面接数字的 在进行运算(算术、逻辑、判断相等)时都会先转化成数值型 #关于数组的测试 /*$i = '01'; $arr = array(1,2); if(array_key_exists($i,$arr)){ echo 'exist'; }else{ echo 'not exist'; } $id = "22"; $arr1[$id] = "xxx"; var_dump($arr1); $id = 22; $arr2[$id] = "xxx"; var_dump($arr2); $id = "022"; $arr3[$id] = "xxx"; var_dump($arr3); $id = "2222222222222"; $arr4[$id] = "xxx"; var_dump($arr4);*/ //结论:key 可以是 integer 或者 string。如果key是一个 integer 的标准表示,则被解释为整数(例如 "8" 将被解释为 8,而 "08" 将被解释为 "08")。key 中的浮点数被取整为 integer。 #用于匹配html文件里的<img/> /*$imgstr = '<img class="home-thumb" src="http://www.daixiaorui.com/Public/images/random/20.jpg" width="140px" height="100px" alt="php源码,小程序"/> <p><img src="http://www.daixiaorui.com/Public/uploads/20130727/chat2.jpg" style="border:1px solid #aaa;" alt="PHP+Ajax"/> <img src=\'http://www.daixiaorui.com/Public/images/random/20.jpg\' width="140px" height="100px" alt="php源码,小程序"/></p> <img src=http://www.daixiaorui.com/Public/images/random/20.jpg />'; $regexp = '/<img\s+([^>]+)(\/>)/i'; preg_match_all($regexp,$imgstr,$res); echo '<pre>'; print_r($res); echo '</pre>';*/ #用于匹配html文件里的<img/>的src的地址 /*$imgstr = '<img class="home-thumb" src="http://www.daixiaorui.com/Public/images/random/20.jpg" width="140px" height="100px" alt="php源码,小程序"/> <p><img src="http://www.daixiaorui.com/Public/uploads/20130727/chat2.jpg" style="border:1px solid #aaa;" alt="PHP+Ajax"/> <img src=\'http://www.daixiaorui.com/Public/images/random/20.jpg\' width="140px" height="100px" alt="php源码,小程序"/></p> <img SRC=http://www.daixiaorui.com/Public/images/random/20.jpg />'; $preg = '/(src|SRC)=(\"|\')?([^>]|[^\/>])+(\"|\')?/i'; preg_match_all($preg,$imgstr,$res); echo '<pre>'; print_r($res); echo '</pre>'; $str = 'asdsadsadsad撒大声地'; $preg= '/[\x{4e00}-\x{9fa5}]+/u'; preg_match($preg,$str,$res); echo '<pre>'; print_r($res); echo '</pre>';*/ #php打印前一天的时间格式是 Y-m-d H:i:s //print(date('Y-m-d H:i:s',time()-3600*24)); //print(date('Y-m-d H:i:s',strtotime('-1 day'))); #求两个日期的差数,例如2011-2-5 ~ 2011 -3-6 // 方法一echo round(abs(strtotime('2011-3-6')- strtotime('2011-2-5'))/3600/24); function date_cha($date1,$date2){ $year1 = (int)substr($date1,0,4); $year2 = (int)substr($date2,0,4); //echo $year1,'---',$year2; $mon1 = (int)substr($date1,strpos($date1,'-')+1,1); $mon2 = (int)substr($date2,strpos($date2,'-')+1,1); //echo $mon1; $day1 = (int)substr($date1,-1); $day2 = (int)substr($date2,-1); //echo $day1; is_run($year1)?$sum_1=nowday($mon1,$day1)+1:$sum_1=nowday($mon1,$day1); is_run($year2)?$sum_2=nowday($mon2,$day2)+1:$sum_2=nowday($mon2,$day2); //echo $sum_1; $res = abs($sum_1-$sum_2); return $res; } function is_run($year){//判断是否是闰年 if((($year%4 == 0) && ($year%100 != 0)) || ($year %400 == 0)){ return true; }else{ return false; } } function nowday($mon,$day){//计算这是这一年的哪一天 $sum = 0; switch($mon){ case 1:$sum = 0;break; case 2:$sum = 31;break; case 3:$sum = 59;break; case 4:$sum = 90;break; case 5:$sum = 120;break; case 6:$sum = 151;break; case 7:$sum = 181;break; case 8:$sum = 212;break; case 9:$sum = 243;break; case 10:$sum = 273;break; case 11:$sum = 304;break; case 12:$sum = 324;break; } $sum += $day; return $sum; } echo date_cha('2011-3-6','2011-2-5');
/*
关于这个的第二个解法 我只解出了相同年的日期
*/
有耕耘、有搬运、共学习