PHP classic interview questions (pure code version)

WBOY
Release: 2016-07-25 08:59:23
Original
779 people have browsed it
  1. /**

  2. * php interview questions
  3. * edit bbs.it-home.org
  4. * at 2013-05-13
  5. */
  6. functiongbk_strrev($str){//----gbk中文字符串翻转-----
  7. $len=strlen($str);
  8. for($i=0;$i<$len;$i++){
  9. $char=$str{0};
  10. if(ord($char)>127){
  11. $i++;
  12. if($i<$len){
  13. $arr[]=substr($str,0,2);
  14. $str=substr($str,2);
  15. }

  16. }else{

  17. $arr[]=$char;
  18. $str=substr($str,1);
  19. }
  20. }
  21. returnimplode(array_reverse($arr));
  22. }

  23. $str=’中文.look!’;

  24. echo gbk_strrev($str);

  25. functionutf8_strrev($string){//-----utf8中文翻转--------

  26. $index=0;

  27. $length=strlen($string);

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

  29. if(ord($first_b)>224){

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

  31. $index+=3;

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

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

  34. $index+=2;

  35. }else{

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

  37. $index+=1;

  38. }

  39. }

  40. returnimplode(array_reverse($arr));

  41. }

  42. $str=’中文.look!’;

  43. echo utf8_strrev($str);

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

  45. $index=0;

  46. $result=’’;

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

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

  49. if(ord($first_b)>127){

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

  51. $index+=2;

  52. }else{

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

  54. $index+=1;

  55. }

  56. }

  57. return$result;

  58. }

  59. $str = "你好china";

  60. echo gbk_substr($str, 5);

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

  62. $index=0;

  63. $result=’’;

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

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

  66. if(ord($first_b)>224){

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

  68. $index+=3;

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

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

  71. $index+=2;

  72. }else{

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

  74. $index+=1;

  75. }

  76. }

  77. return$result;

  78. }

  79. $str = "你好china";

  80. echo (utf8_substr($str, 3));

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

  82. $path_source=opendir($path);

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

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

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

  86. echo$path.’/’.$file,’
    ’;

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

  88. }else{

  89. echo$path.’/’.$file,’
    ’;

  90. }

  91. }

  92. }

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

  94. scan_dirs($dir_name);

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

  96. return strrchr($file_name, ’.’);

  97. }

  98. function get_ext2($file_name){

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

  100. }

  101. function get_ext3($file_name){

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

  103. return array_pop($arr);

  104. }

  105. function get_ext4($file_name){

  106. $p = pathinfo($file_name);

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

  108. }

  109. function get_ext5($file_name){

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

  111. }

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

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

  114. $flag=false;

  115. $count= count($arr);

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

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

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

  119. $tmp=$arr[$j];

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

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

  122. $flag=true;

  123. }

  124. }

  125. if($flag){

  126. $flag=false;

  127. }else{

  128. break;

  129. }

  130. }

  131. return$arr;

  132. }

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

  134. var_dump(maopao($arr));

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

  136. for($i=0;$i

  137. $minIndex=$i;

  138. $minVal=$arr[$i];

  139. for($j=$i+1;$j

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

  141. $minVal=$arr[$j];

  142. $minIndex=$j;

  143. }

  144. }

  145. $tmp=$arr[$i];

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

  147. $arr[$minIndex]=$tmp;

  148. }return$arr;

  149. }

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

  151. var_dump(xuanze($arr));

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

  153. for($i=1;$i

  154. $insertVal=$arr[$i];

  155. $insertIndex=$i-1;

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

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

  158. $insertIndex--;

  159. }

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

  161. }

  162. return$arr;

  163. }

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

  165. var_dump(insertSort($arr));

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

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

  168. $key = $array[0];

  169. $left_arr = array();

  170. $right_arr = array();

  171. for($i=1; $i

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

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

  174. else

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

  176. }

  177. $left_arr = quick_sort($left_arr);

  178. $right_arr = quick_sort($right_arr);

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

  180. }

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

  182. var_dump(quickSort($arr));

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

  184. $flag=false;

  185. for($i=0;$i

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

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

  188. $flag=true;

  189. }

  190. }

  191. if(!$flag){

  192. echo ’找不到’;

  193. }

  194. }

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

  196. seqSch($arr,34);

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

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

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

  200. $midValue=$arr[$midIndex];

  201. if($midValue>$num){

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

  203. }elseif($midValue<$num){

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

  205. }else{

  206. return$midIndex;

  207. }

  208. }

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

  210. $leftIndex=0;

  211. $rightIndex=count($arr);

  212. var_dump(erFen($arr,36,$leftIndex,$rightIndex));

  213. ?>

复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!