Php classic interview questions (code-only version)-php Tutorial

Source: Internet
Author: User
Php classic interview questions (code-only version)

  1. /**

  2. * Php interview questions
  3. * Edit bbs.it-home.org
  4. * At 2013-05-13
  5. */
  6. Functiongbk_strrev ($ str) {// ---- gbk Chinese character string flip -----
  7. $ Len = strlen ($ str );
  8. For ($ I = 0; $ I <$ len; $ I ++ ){
  9. $ Char = $ str {0 };
  10. If (ord ($ char) & gt; 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 = 'Chinese. look! ';

  24. Echo gbk_strrev ($ str );

  25. Functionutf8_strrev ($ string) {// ----- utf8 Chinese flipped --------

  26. $ Index = 0;

  27. $ Length = strlen ($ string );

  28. While ($ first_ B = substr ($ string, $ index, 1 )){

  29. If (ord ($ first_ B) & gt; 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 = 'Chinese. look! ';

  43. Echo utf8_strrev ($ str );

  44. Functiongbk_substr ($ str, $ length) {// ----- gbk intercepts a Chinese string --------

  45. $ Index = 0;

  46. $ Result = '';

  47. For ($ I = 0; $ I <$ length; $ I ++ ){

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

  49. If (ord ($ first_ B) & gt; 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 = "Hello china ";

  60. Echo gbk_substr ($ str, 5 );

  61. Functionutf8_substr ($ string, $ length) {// ----------- utf8-encoded Chinese string -------------

  62. $ Index = 0;

  63. $ Result = '';

  64. For ($ I = 0; $ I <$ length; $ I ++ ){

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

  66. If (ord ($ first_ B) & gt; 224 ){

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

  68. $ Index + = 3;

  69. } Else'if (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 = "Hello china ";

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

  81. Functionscan_dirs ($ path) {// ----- traverse the directory ------------

  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) {// -------------- get the file suffix ----------

  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) {// ------ bubble sort method ------------

  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 );

  134. Var_dump (maopao ($ arr ));

  135. Functionxuanze ($ arr) {// --------- select sort ----------

  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 );

  151. Var_dump (xuanze ($ arr ));

  152. FunctioninsertSort ($ arr) {// ------------ insert sorting method ---------

  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 );

  165. Var_dump (insertSort ($ arr ));

  166. Function quickSort ($ array) {// ----- quick sorting method ----------

  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 );

  182. Var_dump (quickSort ($ arr ));

  183. Function seqSch ($ arr, $ num) {// --- sequential query -----

  184. $ Flag = false;

  185. For ($ I = 0; $ I

  186. If ($ arr [$ I] ==$ num ){

  187. Echo 'found, subscript:'. $ I;

  188. $ Flag = true;

  189. }

  190. }

  191. If (! $ Flag ){

  192. Echo 'couldn't find ';

  193. }

  194. }

  195. $ Arr = array );

  196. SeqSch ($ arr, 34 );

  197. FunctionerFen ($ arr, $ num, $ leftIndex, $ rightIndex) {// ---- binary search --- the prerequisite array ratio is an ordered array ---

  198. If ($ leftIndex >=$ rightIndex) {return 'cannot be found ';}

  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 );

  210. $ LeftIndex = 0;

  211. $ RightIndex = count ($ arr );

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

  213. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.