Common string and file operation questions in PHP interviews PHP flip Chinese string PHP calculate URL file suffix HP calculate the relative path of two files HP traverse all files and folders in the directory
Full range of PHP Video Tutorial: elaborate PHP-http://www.xishuophp.com/
1. PHP flip Chinese Strings
Function reverse ($ str) {$ r = array (); for ($ I = 0; $ I <mb_strlen ($ str); $ I ++) {$ r [] = mb_substr ($ str, $ I, 1, 'utf-8');} return implode (array_reverse ($ r ));} echo reverse ('www .phpha.com PHP blog'); // Result: 'Weibo PHP Zhutian moc. ahphp. wwww'
2. PHP calculates the URL file suffix
Function getext ($ url) {$ data = parse_url ($ url); $ path = $ data ['path']; $ info = pathinfo ($ path ); return $ info ['extension'];} echo getext ('http: // blog.phpha.com/archives/1670.html? Id = 1670 '); // Result: 'html'
3. PHP calculates the relative path of the two files
Function getrpath ($ path, $ conpath) {$ pathArr = explode ('/', $ path); $ conpathArr = explode ('/', $ conpath ); $ dismatchlen = 0; for ($ I = 0; $ I <count ($ pathArr); $ I ++) {if ($ conpathArr [$ I]! = $ PathArr [$ I]) {$ dismatchlen = count ($ pathArr)-$ I; $ arrleft = array_slice ($ pathArr, $ I); break ;}} return str_repeat ('.. /', $ dismatchlen ). implode ('/', $ arrleft);} $ a = '/a/B/c/d/e. php '; $ B ='/a/B/12/34/5. php '; echo getrpath ($ a, $ B); // Result :'.. /.. /.. /c/d/e. php'
4. PHP traverses all files and folders in the directory
Function finddir ($ dir) {$ files = array (); if (is_dir ($ dir) {if ($ handle = opendir ($ dir )) {while ($ file = readdir ($ handle ))! = False) {if ($ file! = '.' & $ File! = '.. ') {If (is_dir (rtrim ($ dir ,'/'). '/'. $ file) {$ files [$ file] = finddir (rtrim ($ dir ,'/'). '/'. $ file);} else {$ files [] = rtrim ($ dir ,'/'). '/'. $ file ;}}closedir ($ handle) ;}return $ files ;}print_r (finddir ('F:/Golang/src ');/** result: array ([0] => F:/Golang/src/hello. go [1] => F:/Golang/src/src.exe [test] => Array ([0] => F:/Golang/src/test/sss.txt )) **/