Home >Backend Development >PHP Tutorial >PHP entry-level interview questions: Programming questions (1)
PHP Junior Interview Questions are for programmers with little experience who are just looking for a job. This provides a lot of help for us to go out for interview, InterviewOfficial meetings often test us, and the interview questions played a big role at this time.
1. Write a function to retrieve the file extension from a standard URL as efficiently as possible
For example: http:/ /www.php.cn/course.html Need to remove html or .html
Answer one:
function getExt($url){ $arr = parse_url($url); $file = basename($arr['path']); $ext = explode(".",$file); return $ext[1]; }
Answer two:
function getExt($url) { $url = basename($url); $pos1 = strpos($url,"."); $pos2 = strpos($url,"?"); if(strstr($url,"?")){ return substr($url,$pos1 + 1,$pos2 - $pos1 - 1); } else { return substr($url,$pos1); } }
2. In HTML language, the meta tag at the head of the page can be used to output the encoding format of the file. The following is a standard meta statement
Please use PHP language to write a function to change the charset part value in a similar meta tag in a standard HTML page to big5
Please note:
1. Need to process the complete html page, that is, not just this meta statement
2. Ignore the case
3. ' and " are interchangeable here
4. The quotation marks on both sides of 'Content-Type' can be ignored, but the quotation marks on both sides of 'text/html; charset=gbk' It doesn’t work
5. Pay attention to dealing with extra spaces
3. Write a function to calculate the relative relationship between the two files Path
For example,
$a = '/a/b/c/d/e.php'; $b = '/a/b/12/34/c.php';
calculates that the relative path of $b relative to $a should be ../../c/d and add () to
Answer:
function getRelativePath($a, $b) { $returnPath = array(dirname($b)); $arrA = explode('/', $a); $arrB = explode('/', $returnPath[0]); for ($n = 1, $len = count($arrB); $n < $len; $n++) { if ($arrA[$n] != $arrB[$n]) { break; } } if ($len - $n > 0) { $returnPath = array_merge($returnPath, array_fill(1, $len - $n, '..')); } $returnPath = array_merge($returnPath, array_slice($arrA, $n)); return implode('/', $returnPath); } echo getRelativePath($a, $b);
4. Write a function that can traverse all files and subfolders in a folder.
Answer:
function my_scandir($dir) { $files = array(); if ( $handle = opendir($dir) ) { while ( ($file = readdir($handle)) !== false ) { if ( $file != ".." && $file != "." ) { if ( is_dir($dir . "/" . $file) ) { $files[$file] = scandir($dir . "/" . $file); }else { $files[] = $file; } } } closedir($handle); return $files; } }
Don’t worry after reading the above interview questions. There are other interview questions. It is best to consolidate the basic things to help us interview and find a job.
Related recommendations:
phpJunior Interview Questions Brief Description Questions (1)
php Junior Interview Questions Brief Description Questions (2)
php Junior Interview Questions Brief Description Questions (3)
phpJunior Interview Questions Brief Description Questions (4)
The above is the detailed content of PHP entry-level interview questions: Programming questions (1). For more information, please follow other related articles on the PHP Chinese website!