For example: http://www.sina.com.cn/abc/de/fg.php?id=1 need to remove PHP or. php
Scenario 1
<?php
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
<?php
$file="http://www.sina.com.cn/abc/de/fg.php?id=1";
$str=pathinfo($file,PATHINFO_EXTENSION);
echo strchr($str,"?",true);
PathInfo ()--returns information about a file path
PathInfo
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
<?php
print_r(pathinfo("/testweb/test.txt"));
?>
The above code will output:
Array (
[dirname] => /testweb
[basename] => test.txt
[extension] => txt
[filename] => test
)
Example 2
<?php
var_dump(pathinfo("/testweb/test.txt",PATHINFO_DIRNAME));
var_dump(pathinfo("/testweb/test.txt",PATHINFO_BASENAME));
var_dump(pathinfo("/testweb/test.txt",PATHINFO_EXTENSION));
?>
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:
string(8)"/testweb"
string(8)"test.txt"
string(3)"txt"
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.
Parameters
url:The URL to resolve. Invalid characters will be replaced with _.
component:
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
<?php
$url = ‘http://username:[email protected]/path?arg=value#anchor‘;
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
The above routines will output:
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
/path
Example #2 Parse_url () example of parsing a lost protocol
<?php
$url = ‘//www.example.com/path?googleguy=googley‘;
// before 5.4.7 will output path "//www.example.com/path"
var_dump(parse_url($url));
?>
The above routines will output:
array(3) {
["host"]=>
string(15) "www.example.com"
["path"]=>
string(5) "/path"
["query"]=>
string(17) "googleguy=googley"
}
Notes
Note:
This function cannot be used with relative URLs.
Note:
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. |
Example:
<? PHP
$path = "/testweb/home.php";
//Show file names with file extensions
echo basename($path);
//Show file names without file extensions
echo basename($path,".php");
> >
Output:
Home. Phphome
http://php.net/manual/zh/function.parse-url.php
Http://www.w3school.com.cn/php/func_filesystem_basename.asp
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