Several PHP basic questions

Source: Internet
Author: User
Preface last night, a lab teacher @ me on the meager page, sent me some basic questions about PhP, and posted the answers I wrote here (1) write a function to get the file Suffix of the URL, such as "http://www.feiyan.info/test.php? C = Class & M = method "(obtain PHP or. php)
<? PHP/*** get the file suffix * @ Param string $ URL * @ return string */function geturlpostfix ($ URL) {$ url_arr = explode ('. ', $ URL); $ Postfix = $ url_arr [count ($ url_arr)-1]; $ substr = substr ($ Postfix, 0, 3); return $ substr ;} $ url = "http://www.feiyan.info/test.php? C = Class & M = method "; $ STR = geturlpostfix ($ URL); echo $ Str." \ n ";
(2) Write a function and add a comma to a string every three characters. For example, convert string 1234567890 to 1,234,567,890 (Accounting Method Used in finance)
<? PHP/*** separated by commas (,) * @ Param string $ Str * @ return string */function splitstrwithcomma ($ Str) {$ arr = array (); $ Len = strlen ($ Str); For ($ I = $ len-1; $ I >=0;) {$ new_str = ""; for ($ J = $ I; $ j> $ I-3 & $ j> = 0; $ j --) {$ new_str. = $ STR [$ J] ;}$ arr [] = $ new_str; $ I = $ J ;}$ string = implode (',', $ ARR ); // flip the string to implement it by yourself // $ string = strrev ($ string); For ($ I = 0, $ J = strlen ($ string)-1; $ I <= $ J; $ I ++, $ j --) {$ TMP = $ string [$ I]; $ string [$ I] = $ string [$ J]; $ string [$ J] = $ TMP;} return $ string ;}$ STR = "1234567890 "; $ new_str = splitstrwithcomma ($ Str); echo $ new_str. "\ n ";

(3) Write a PHP function to calculate the relative paths of the two files. For example, $ A = "/A/B/C/D/E. PHP "; $ B ="/a/B/12/34/C. PHP ", what is the relative path of B to? This question can be regarded as the first public node question. Most of the Code circulating on the internet is wrong, and it is not comprehensive. Of course, I only use ".. /"to indicate, useless ". /"

<? PHP/*** calculates the relative path of $ B relative to $ A * @ Param string $ B * @ return string */function getrelativepath ($, $ B) {$ patha = explode ('/', $ A); $ pathb = explode ('/', $ B); $ counta = count ($ patha) -1; $ countb = count ($ pathb)-1; $ Path = ".. /"; if ($ countb> $ counta) {While ($ countb> $ counta) {$ path. = ".. /"; $ countb -- ;}/// find the first public node for ($ I = $ countb-1; $ I >=0 ;) {if ($ patha [$ I]! = $ Pathb [$ I]) {$ path. = ".. /"; $ I --;} else {// determine whether it is the real first public node to prevent the occurrence of sub-directory name duplication. For ($ J = $ I-1, $ flag = 1; $ j> = 0; $ j --) {if ($ patha [$ J] = $ pathb [$ J]) {continue ;} else {$ flag = 0; break ;}} if ($ flag) break; else $ I ++ ;}}for ($ I + = 1; $ I <= $ counta; $ I ++) {$ path. = $ patha [$ I]. "/";} return $ path;} $ A = "/A/C/D/E. PHP "; $ B ="/a/c. PHP "; $ Path = getrelativepath ($ A, $ B); echo $ path;

(4) calculate the number of days between two dates

<? PHP/*** calculate the number of days between two dates (the Taylor formula can be used before the calculation after January 1, January 1, 1970) * @ Param string $ day1 * @ Param string $ day2 * @ return number */function diffbetweentwodays ($ day1, $ day2) {$ second1 = strtotime ($ day1 ); $ second2 = strtotime ($ day2); if ($ second1 <$ second2) {$ TMP = $ second2; $ second2 = $ second1; $ second1 = $ TMP ;} return ($ second1-$ second2)/86400;} $ day1 = "2013-07-27"; $ day2 = "2013-08-04"; $ diff = diffbetweentwodays ($ day1, $ day2 ); echo $ diff. "\ n ";

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.