Method One:
function Getext ($url) {$urlinfo = parse_url ($url); $file = basename ($urlinfo [' path ']); if (Strpos ($file, '. ')!== false) {$ext = explode ('. ', $file); return $ext [Count ($ext)-1]; } return ' no extension ';}
URL of the test:
echo getext (' http://www.sina.com.cn/abc/de/fg.php?id=1 '), "<br>"; Echo getext (' http://china.cankaoxiaoxi.com /2014/0629/407885.shtml '), "<br>"; Echo getext (' http://tieba.baidu.com/p/3137855685 '), "<br>"; Echo Getext (' http://www.oschina.net/code/list/4/php?lang=php '), "<br>"; Echo getext (' http://news.qq.com/a/ 20140701/013380.htm? Aduin=1281256312&adsession=1404165481&adtag=client. qq.5335_.0&adpubno=26361 '), "<br>";
Results:
Phpshtmlno Extensionno extensionhtm
Method Two:
function GetExt2 ($url) {
$urlinfo = PathInfo ($url); if (Isset ($urlinfo [' extension '])) {if (Strpos ($urlinfo [' extension '], '? ')!== false) return explode ('? ', $urlinfo [' E XTension ']) [0]; else return $urlinfo [' extension ']; } return ' no extension ';}
Test URL Ibid.
Results:
Phpshtmlno Extensionno extension0&adpubno=26361
The last URL extension gets a problem, and I understand that pathinfo ($url) extension is based on whether the URL exists "." To obtain, if "." is present, the extension should be intercepted at a location after the last point.