Preface
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]
<? 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 ";
<? 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 ";
(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]
<? 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 ";
<? 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 ";
(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]
<? 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;
<? 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;
(4) calculate the number of days between two dates
[Php]
<? 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 ";
<? 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 ";