PHP gets the extension of the URL

Source: Internet
Author: User
Tags explode

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.