Home >Backend Development >PHP Tutorial >PHP几个比较常见的面试程序题整理

PHP几个比较常见的面试程序题整理

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-23 13:28:051125browse

反转字符串可以使用【strrev】但是最终要的就是多字节字符串

	//反转字符串	function mb_strrev($str){		$len = mb_strlen($str,'utf-8');		$r = array();		for($i=0;$i<$len;$i++){			$r[] = mb_substr($str,$i,1,'utf-8');		}		return implode(array_reverse($r));	}

得到URL中扩展名,注意URL中不一定有扩展名的

	//得到url中扩展名	function getUrlExt($str){		$url_info = parse_url($str);		if(array_key_exists('path',$url_info)){			$path = $url_info['path'];			$file_info = pathinfo($path);			if(array_key_exists('extension',$file_info)){				return $file_info['extension'];			}		}		return false;		}


计算两个文件的相对路径

	function getrpath($path,$conpath){		$pathArr = explode('/',$path);		$conpathArr = explode('/',$conpath);		//$dis_match_len = 0;		for($i=0;$i<count($pathArr);$i++){			if($pathArr[$i] != $conpathArr[$i]){				$dis_match_len = count($pathArr) - $i - 1;				$arr_left = array_slice($pathArr,$i);				break;			}		}		return str_repeat('../',$dis_match_len).implode('/',$arr_left);	}

计算两个文件相对路径的方法2,使用PHP内置函数【array_diff_assoc】

	function getrpath2($path,$conpath){		$pathA = explode('/',$path);		$pathB = explode('/',$conpath);		$res = array_diff_assoc($pathA,$pathB);		$path = '';		for($i=0;$i<count($res)-1;$i++){			$path .= '../';		}		$path .= implode('/',$res);		return $path;	}


版权声明:本文为博主原创文章,未经博主允许不得转载。

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