/**
- * Php interview questions
- * Edit bbs.it-home.org
- * At 2013-05-13
- */
- Functiongbk_strrev ($ str) {// ---- gbk Chinese character string flip -----
- $ Len = strlen ($ str );
- For ($ I = 0; $ I <$ len; $ I ++ ){
- $ Char = $ str {0 };
- If (ord ($ char) & gt; 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 = 'Chinese. look! ';
Echo gbk_strrev ($ str );
Functionutf8_strrev ($ string) {// ----- utf8 Chinese flipped --------
- $ Index = 0;
$ Length = strlen ($ string );
While ($ first_ B = substr ($ string, $ index, 1 )){
If (ord ($ first_ B) & gt; 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 = 'Chinese. look! ';
Echo utf8_strrev ($ str );
Functiongbk_substr ($ str, $ length) {// ----- gbk intercepts a Chinese string --------
$ Index = 0;
$ Result = '';
For ($ I = 0; $ I <$ length; $ I ++ ){
$ First_ B = substr ($ str, $ index, 1 );
If (ord ($ first_ B) & gt; 127 ){
$ Result. = substr ($ str, $ index, 2 );
$ Index + = 2;
} Else {
$ Result. = substr ($ str, $ index, 1 );
$ Index + = 1;
}
}
Return $ result;
}
$ Str = "Hello china ";
Echo gbk_substr ($ str, 5 );
Functionutf8_substr ($ string, $ length) {// ----------- utf8-encoded Chinese string -------------
$ Index = 0;
$ Result = '';
For ($ I = 0; $ I <$ length; $ I ++ ){
$ First_ B = substr ($ string, $ index, 1 );
If (ord ($ first_ B) & gt; 224 ){
$ Result. = substr ($ string, $ index, 3 );
$ Index + = 3;
} Else'if (ord ($ first_ B> 192 )){
$ Result. = substr ($ string, $ index, 2 );
$ Index + = 2;
} Else {
$ Result. = substr ($ string, $ index, 1 );
$ Index + = 1;
}
}
Return $ result;
}
$ Str = "Hello china ";
Echo (utf8_substr ($ str, 3 ));
Functionscan_dirs ($ path) {// ----- traverse the directory ------------
$ 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 ,'
';
Scan_dirs ($ path. '/'. $ file );
} Else {
Echo $ path. '/'. $ file ,'
';
}
}
}
$ Dir_name = 'E:/amp/apache/htdocs/mvc ';
Scan_dirs ($ dir_name );
Function get_ext1 ($ file_name) {// -------------- get the file suffix ----------
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) {// ------ bubble sort method ------------
$ 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 );
Var_dump (maopao ($ arr ));
Functionxuanze ($ arr) {// --------- select sort ----------
For ($ I = 0; $ I
$ MinIndex = $ I;
$ MinVal = $ arr [$ I];
For ($ j = $ I + 1; $ j
If ($ minVal> $ arr [$ j]) {
$ MinVal = $ arr [$ j];
$ MinIndex = $ j;
}
}
$ Tmp = $ arr [$ I];
$ Arr [$ I] = $ arr [$ minIndex];
$ Arr [$ minIndex] = $ tmp;
} Return $ arr;
}
$ Arr = array );
Var_dump (xuanze ($ arr ));
FunctioninsertSort ($ arr) {// ------------ insert sorting method ---------
For ($ I = 1; $ 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 );
Var_dump (insertSort ($ arr ));
Function quickSort ($ array) {// ----- quick sorting method ----------
If (count ($ array) <= 1) return $ array;
$ Key = $ array [0];
$ Left_arr = array ();
$ Right_arr = array ();
For ($ I = 1; $ 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 );
Var_dump (quickSort ($ arr ));
Function seqSch ($ arr, $ num) {// --- sequential query -----
$ Flag = false;
For ($ I = 0; $ I
If ($ arr [$ I] ==$ num ){
Echo 'found, subscript:'. $ I;
$ Flag = true;
}
}
If (! $ Flag ){
Echo 'couldn't find ';
}
}
$ Arr = array );
SeqSch ($ arr, 34 );
FunctionerFen ($ arr, $ num, $ leftIndex, $ rightIndex) {// ---- binary search --- the prerequisite array ratio is an ordered array ---
If ($ leftIndex >=$ rightIndex) {return 'cannot be found ';}
$ 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 );
$ LeftIndex = 0;
$ RightIndex = count ($ arr );
Var_dump (erFen ($ arr, 36, $ leftIndex, $ rightIndex ));
- ?>