php经典面试题(纯代码版)

发布时间:2020-06-09编辑:脚本学堂
本文介绍一个,php的经典面试题,只提供了可能的代码,有需要的朋友,不妨参考下。
专题推荐:php面试题及答案

代码如下:
 

复制代码 代码示例:

<?php
/**
* php 面试题
* edit www.jb200.com
* at 2013-05-13
*/
functiongbk_strrev($str){//----gbk中文字符串翻转-----
$len=strlen($str);
for($i=0;$i<$len;$i++){
   $char=$str{0};
   if(ord($char)>127){
       $i++;
       if($i<$len){
          $arr[]=substr($str,0,2);
         $str=substr($str,2);
     }

   }else{
  $arr[]=$char;
     $str=substr($str,1);
     }
  }
  returnimplode(array_reverse($arr));
 }

$str=’中文.look!’;

echo gbk_strrev($str);

functionutf8_strrev($string){//-----utf8中文翻转--------
         $index=0;

         $length=strlen($string);

                   while($first_b=substr($string,$index,1)){

                   if(ord($first_b)>224){

                            $arr[]=substr($string,$index,3);

                            $index+=3;

                   }elseif(ord($first_b)>192){

                            $arr[]=substr($string,$index,2);

                            $index+=2;

                   }else{

                            $arr[]=substr($string,$index,1);

                            $index+=1;

                            }

                   }

                   returnimplode(array_reverse($arr));

         }

$str=’中文.look!’;

echo utf8_strrev($str);

functiongbk_substr($str,$length){//-----gbk截取中文字符串--------

         $index=0;

         $result=’’;

         for($i=0;$i<$length;$i++){

                   $first_b=substr($str,$index,1);

                   if(ord($first_b)>127){

                            $result.= substr($str,$index,2);

                            $index+=2;

                   }else{

                            $result.= substr($str,$index,1);

                            $index+=1;

                            }

                   }

                   return$result;

         }

$str = "你好china";

echo gbk_substr($str, 5);

functionutf8_substr($string,$length){//-----------utf8编码截取中文字符串-------------

         $index=0;

         $result=’’;

         for($i=0;$i<$length;$i++){

                   $first_b=substr($string,$index,1);

                   if(ord($first_b)>224){

                            $result.= substr($string,$index,3);

                            $index+=3;

                   }elseif(ord($first_b>192)){

                            $result.= substr($string,$index,2);

                            $index+=2;

                   }else{

                            $result.= substr($string,$index,1);

                            $index+=1;

                            }

                   }

                   return$result;

         }

$str = "你好china";

echo (utf8_substr($str, 3));

functionscan_dirs($path){//-----遍历目录------------

         $path_source=opendir($path);

         while(($file=readdir($path_source))!==false){

                   //if(is_dir($path.’/’.$file)&&$file!= ’.’&&$file !=’..’){

                   if(is_dir($path. ’/’ . $file) && $file != ’.’ && $file != ’..’) {

                            echo$path.’/’.$file,’<br>’;

                            scan_dirs($path.’/’.$file);

                   }else{

                            echo$path.’/’.$file,’<br>’;

                            }

                   }

         }

$dir_name = ’E:/amp/apache/htdocs/mvc’;

scan_dirs($dir_name);

function get_ext1($file_name){//--------------获取文件后缀名----------

return strrchr($file_name, ’.’);

}

function get_ext2($file_name){

return substr($file_name,strrpos($file_name, ’.’));

}

function get_ext3($file_name){

$arr=explode(’.’, $file_name);

return array_pop($arr);

}

function get_ext4($file_name){

$p = pathinfo($file_name);

return$p[’dirname’].’------’.$p[’basename’].’------’.$p[’extension’];

}

function get_ext5($file_name){

return strrev(substr(strrev($file_name), 0,strpos(strrev($file_name), ’.’)));

}

echoget_ext5(’E:/amp/apache/htdocs/mvc/init.html’);

functionmaopao($arr){//------冒泡排序法------------

         $flag=false;

         $count= count($arr);

         for($i=0;$i<$count-1;$i++){

                   for($j=0;$j<$count-1-$i;$j++){

                            if($arr[$j]>$arr[$j+1]){

                                     $tmp=$arr[$j];

                                     $arr[$j]=$arr[$j+1];

                                     $arr[$j+1]=$tmp;

                                     $flag=true;

                                     }

                            }

                            if($flag){

                                     $flag=false;

                            }else{

                                     break;

                                     }

                   }

                   return$arr;

         }

         $arr=array(12,78,49,68,59,67,93,34,46,33);

         var_dump(maopao($arr));

functionxuanze($arr){//---------选择排序----------

         for($i=0;$i<count($arr);$i++){

                   $minIndex=$i;

                   $minVal=$arr[$i];

                   for($j=$i+1;$j<count($arr);$j++){

                            if($minVal>$arr[$j]){

                                     $minVal=$arr[$j];

                                     $minIndex=$j;

                                     }

                            }

                            $tmp=$arr[$i];

                            $arr[$i]=$arr[$minIndex];

                            $arr[$minIndex]=$tmp;

                   }return$arr;

         }

         $arr=array(12,78,49,68,59,67,93,34,46,33);

         var_dump(xuanze($arr));

functioninsertSort($arr){//------------插入排序法---------

         for($i=1;$i<count($arr);$i++){

                   $insertVal=$arr[$i];

                   $insertIndex=$i-1;

                   while($insertIndex>=0&&$insertVal<=$arr[$insertIndex]){

                            $arr[$insertIndex+1]=$arr[$insertIndex];

                            $insertIndex--;

                            }

                            $arr[$insertIndex+1]=$insertVal;

                   }

                   return$arr;

         }

         $arr=array(12,78,49,68,59,67,93,34,46,33);

         var_dump(insertSort($arr));

function quickSort($array){//-----快速排序法----------

  if(count($array) <= 1) return $array;

 

 $key = $array[0]; 

 $left_arr = array(); 

 $right_arr = array();

 

  for($i=1; $i<count($array); $i++){ 

         if ($array[$i] <= $key) 

                  $left_arr[] = $array[$i]; 

         else 

                  $right_arr[] = $array[$i]; 

  }

 

 $left_arr = quick_sort($left_arr); 

 $right_arr = quick_sort($right_arr);

 

 return array_merge($left_arr, array($key), $right_arr); 

}

         $arr=array(12,78,49,68,59,67,93,34,46,33);

         var_dump(quickSort($arr));

function seqSch($arr, $num){//---顺序查找-----

         $flag=false;

 for($i=0;$i<count($arr);$i++){

       if($arr[$i]==$num){

                echo ’找到了,下标为:’.$i;

                $flag=true;

                }

       }

 if(!$flag){

       echo ’找不到’;

       }

}

$arr=array(12,78,49,68,59,67,93,34,46,33);

seqSch($arr,34);

functionerFen($arr,$num,$leftIndex,$rightIndex){//----二分查找---前提数组比为有序数组---

         if($leftIndex>=$rightIndex){return’找不到’;}

         $midIndex=floor(($leftIndex+$rightIndex)/2);

         $midValue=$arr[$midIndex];

         if($midValue>$num){

                   returnerFen($arr,$num,$leftIndex,$midIndex-1);

         }elseif($midValue<$num){

                   returnerFen($arr,$num,$midIndex+1,$rightIndex);

         }else{

                   return$midIndex;

                   }

         }

$arr=array(3,5,7,8,9,23,26,36);

$leftIndex=0;

$rightIndex=count($arr);

var_dump(erFen($arr,36,$leftIndex,$rightIndex));
?>