Home  >  Article  >  Backend Development  >  PHP programmer interview questions (classic summary)

PHP programmer interview questions (classic summary)

WBOY
WBOYOriginal
2016-07-25 08:59:27947browse
  1. function GBsubstr($string, $start, $length) {
  2. if(strlen($string)>$length){
  3. $str=null;
  4. $len=$start+$length;
  5. for( $i=$start;$i<$len;$i++){
  6. if(ord(substr($string,$i,1))>0xa0){
  7. $str.=substr($string,$i, 2);
  8. $i++;
  9. }else{
  10. $str.=substr($string,$i,1);
  11. }
  12. }
  13. return $str.'...';
  14. }else{
  15. return $string ;
  16. }
  17. }
Copy the code

2. Use PHP to write the code to display the client IP and server IP? Answer:

  1. $readcontents = fopen("http://bbs.it-home.org/index.html", "rb");
  2. $contents = stream_get_contents($readcontents);
  3. fclose($ readcontents);
  4. echo $contents;
Copy code

Method 2:

  1. function getExt($url){
  2. $arr = parse_url($url);
  3. $file = basename($arr['path']);
  4. $ext = explode(".",$ file);
  5. return $ext[1];
  6. }
Copy code

Answer 2:

  1. function getExt($url) {
  2. $url = basename($url);
  3. $pos1 = strpos($url,".");
  4. $pos2 = strpos($url,"?") ;
  5. if(strstr($url,"?")){
  6. return substr($url,$pos1 + 1,$pos2 - $pos1 - 1);
  7. } else {
  8. return substr($url,$pos1) ;
  9. }
  10. }
Copy code

19. Write a function to calculate the relative paths of two files? For example $a = '/a/b/c/d/e.php'; ​$b = '/a/b/12/34/c.php'; Calculate that the relative path of $b relative to $a should be http://bbs.it-home.org/c/d and add () Answer:

  1. function getRelativePath($a, $b) {
  2. $returnPath = array(dirname($b));
  3. $arrA = explode('/', $a);
  4. $arrB = explode ('/', $returnPath[0]);
  5. for ($n = 1, $len = count($arrB); $n < $len; $n++) {
  6. if ($arrA[$n] ! = $arrB[$n]) {
  7. break;
  8. }
  9. }
  10. if ($len - $n > 0) {
  11. $returnPath = array_merge($returnPath, array_fill(1, $len - $n, '. .'));
  12. }
  13. $returnPath = array_merge($returnPath, array_slice($arrA, $n));
  14. return implode('/', $returnPath);
  15. }
  16. echo getRelativePath($a, $b );
Copy code

I hope the above PHP interview questions provided for you will be helpful to you, and I sincerely hope that they can be used in your application.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn