標籤:style blog http color os io 使用 ar 檔案
1.二分尋找演算法
function two_find($arr,$low,$height,$k){ if($low<=$height){ $mid = intval(($low+$height)/2); if($arr[$mid]==$k){ return true; }elseif($k<$arr[$mid]){ return two_find($arr,$low,$mid-1,$k); }else{ return two_find($arr,$mid+1,$height,$k); } } return false;}$arr = array(1,3,4,5,6,9,10,15,20,25,27,30);$key = 20;if(two_find($arr,min(array_keys($arr)),max(array_keys($arr)),$key)){ echo "二分尋找成功!";}else{ echo "二分尋找失敗!";}View Code
2.一個字串中含有字母和數字,請輸出所有數位和,注意,連續的數字算一個數字。(比如輸入字串為“23abc23dbf23” 最後輸入的值應該為:69)
function str_sum($str){ $reg = ‘/[1-9]+[0-9]+/‘; preg_match_all($reg,$str,$marth); if(empty($marth)){ return false; } return array_sum($marth[0]);}echo str_sum("12adb012add012");View Code
3.php從url取出副檔名,儘可能高效和簡單。
function get_url($url){ $path = pathinfo($url); $extenstion = explode("?",$path[‘extension‘]); return $extenstion[0];}echo ‘從url取出副檔名,url為:http://m.cn/asdfasdf/2.asdf.adsfa.php?a=bbb&c=adfas,他的副檔名為:‘.get_url(‘http://m.cn/asdfasdf/2.asdf.adsfa.php?a=bbb&c=adfas‘);View Code
4.php中控制頁面的顯示狀態為404,請寫出PHp代碼
echo "php中用header()函數是可以為返回頁面添加404的頭資訊的,從而提示瀏覽器該網頁找不到了。所以可以使用:header(‘HTTP/1.0 404 Not Found‘);或者:header(‘Status: 404 Not Found‘);後者是在FastCGI模式下使用的,在php代碼中可以把兩句直接同時寫上。";
PHP之幾道面試題