Several php basic questions _ PHP tutorials

Source: Internet
Author: User
Several php basic questions. 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.
Last night, a lab teacher @ me on the meager pages and sent me some basic php interview questions. here I posted my answers.


Question


(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 for a given url
* @ 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 ";

/**
* Get the file suffix for a given url
* @ 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 (,) every 3 characters
* @ 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 ";

/**
* Separated by commas (,) every 3 characters
* @ 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]

/**
* Calculate the relative path of $ B relative to $.
* @ Param string $
* @ Param string $ B
* @ Return string
*/
Function getRelativePath ($ a, $ B)
{
$ Patha = explode ('/', $ );
$ 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 duplicate sub-directories
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;

/**
* Calculate the relative path of $ B relative to $.
* @ Param string $
* @ Param string $ B
* @ Return string
*/
Function getRelativePath ($ a, $ B)
{
$ Patha = explode ('/', $ );
$ 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 duplicate sub-directories
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 ";

/**
* 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 ";

Listen last night, a lab teacher @ me on the meager page, sent me some basic php interview questions, and posted the answers I wrote here (1) write a function to get the file suffix of the URL ,...

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.