如何從下面的標準URL中獲取後綴名,要求儘量高效: php
http://www.baidu.com/index.php?id=1234&a=qwer
用PHP來實現,主要有兩個函數能夠提供解決方案。 html
一、pathinfo()函數,實現代碼以下: 函數
$url = 'http://www.baidu.com/index.php?id=1234&a=qwer'; $array = pathinfo($url); $extension = $array['extension']; $position = strpos($extension, '?'); $result = $position === false ? $extension : substr($extension, 0, $position);
獲得的結果是:php url
二、parse_url()函數,實現代碼以下: code
$url = 'http://www.baidu.com/index.php?id=1234&a=qwer'; $array = parse_url($url); $path = $array['path']; $result = substr($path, strpos($path, '.'));
獲得的結果是:.php htm
兩種方式原理都是相同的:解析URL + 字符串截取 字符串