Common string and file operation questions during PHP interviews

Source: Internet
Author: User
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 )) **/

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.