Write a function that removes the file extension from a standard URL as efficiently as possible? PathInfo file path & Parse_url resolve URL & file name in BaseName path

Source: Internet
Author: User

For example: http://www.sina.com.cn/abc/de/fg.php?id=1 need to remove PHP or. php

Scenario 1

function getExt($url){
    $arr = parse_url($url);
    $file = basename($arr[‘path‘]);
    $ext = explode(".",$file);
    return $ext[1];
echo getExt("http://www.sina.com.cn/abc/de/fg.php?id=1");

Output Result: PHP

Scenario 2

echo strchr($str,"?",true);
PathInfo ()--returns information about a file path
Parameters Description
Path Necessary. Specifies the path to check.
Options Optional. Specifies the array element to return. The default is all.

Possible values:

  • Pathinfo_dirname-Return only DIRNAME
  • Pathinfo_basename-Return only BASENAME
  • Pathinfo_extension-Return only EXTENSION
Definition and usage

The PathInfo () function returns information about the file path in the form of an array or string.

The returned array elements are as follows:

    • [DirName]: Returns the directory portion of the file path
    • [basename]: Returns the part of the file name in the path
    • [Extension]: Returns the part of the file type in the file path
Hints and Notes

Note: If not all elements are requested, the PathInfo () function returns a string.

PHP turns on pathinfo routing mode:pathinfo mode requires PHP. INI to open the following parameter CGI. fix_pathinfo=1path_info mode: http://www.xxx.com/index.php/Module/method 

 Example 1 

The above code will output:

Array (
    [dirname] => /testweb
    [basename] => test.txt
    [extension] => txt
    [filename] => test
Example 2

Note: Pathinfo_extension cannot be enclosed in double quotes "" or single quotation marks.

Warning:Warning: pathinfo () expects parameter 2 to being long, string given in C:\AppServ\www\test.php on Li NE 3

The above code will output:

parse_url- Parse URL, return its components
Mixed Parse_url string $url $component =-1])

This function parses a URL and returns an associative array containing the various components that appear in the URL.

This function is not used to validate the legitimacy of a given URL , but to decompose it into the parts listed below. An incomplete URL is also accepted, andParse_url () tries to parse it as correctly as possible.


url:The URL to resolve. Invalid characters will be replaced with _.


Specifies,,,,,, or one ofPHP_URL_SCHEMEPHP_URL_HOSTPHP_URL_PORTPHP_URL_USERPHP_URL_PASSPHP_URL_PATHPHP_URL_QUERYPHP_URL_FRAGMENTThe strings that gets the specified part of the URL . (PHP_URL_PORTAn integer value is returned, except as specified .)

return value

For a severely unqualified URL,parse_url () may be returnedFALSE.

If the argument is omittedcomponent, an associative array of arrays is returned , at least one element is currently in the array. There are several possible keys in the array:

Scheme- like Httphostportuserpasspathquery -in question marks? after fragment - after the hash symbol #

If a parameter is specifiedcomponent, parse_url () returns a string (orPHP_URL_PORTreturns an integer when specified ) instead of an array. If the component specified in the URL does not exist, it will be returnedNULL. Example Analysis:

<? PHP Print_r (parse_url("https://www.so.com/s?q=strpos&src=360chrome_zoned"));


Output Result:

Array (
    [scheme] => https
    [host] => www.so.com
    [path] => /s
    [query] => q=strpos&src=360chrome_zoned

Example 2

<? PHP Print_r (parse_url("https://i.cnblogs.com/EditPosts.aspx?opt=1"));

Operation Result:

Array (
    [scheme] => https
    [host] => i.cnblogs.com
    [path] => /EditPosts.aspx
    [query] => opt=1 )

Example #1 Parse_url () example

$url = ‘http://username:[email protected]/path?arg=value#anchor‘;


echo parse_url($url, PHP_URL_PATH);

The above routines will output:

    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor

Example #2 Parse_url () example of parsing a lost protocol

$url = ‘//www.example.com/path?googleguy=googley‘;

// before  5.4.7 will output path "//www.example.com/path"

The above routines will output:

array(3) {
  string(15) "www.example.com"
  string(5) "/path"
  string(17) "googleguy=googley"


This function cannot be used with relative URLs.


Parse_url () is specifically used to parse URLs instead of URIs. However, there is an exception to the need for backward compatibility with PHP, which allows three slashes (file:///) for the file://protocol. )。 No other protocol can do that.

BaseName ()--the function returns the file name portion of the path.
basename (Path,suffix)
Parameters Description
Path Necessary. Specifies the path to check.
Suffix Optional. Specifies the file name extension. If the file has suffix, the extension is not exported.


<? PHP
$path = "/testweb/home.php";
//Show file names with file extensions
echo basename($path);
//Show file names without file extensions
echo basename($path,".php");
> > 


Home. Phphome



Write a function that removes the file extension from a standard URL as efficiently as possible? PathInfo file path & Parse_url resolve URL & file name in BaseName path

Related Article

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.