寫一個函數,儘可能高效的,從一個標準 url 裡取出檔案的副檔名?pathinfo檔案路徑& parse_url解析url & basename路徑中檔案名稱

來源:互聯網
上載者:User

標籤:post   pos   符號   comment   inline   指定   tab   相容   lock   

例如: http://www.sina.com.cn/abc/de/fg.php?id=1 需要取出 php 或 .php

方案1

<?phpfunction 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");

輸出結果:php

 

方案2

<?php$file="http://www.sina.com.cn/abc/de/fg.php?id=1";$str=pathinfo($file,PATHINFO_EXTENSION);echo strchr($str,"?",true);
pathinfo() ——返迴文件路徑的資訊
 pathinfo(path,options) 
參數 描述
path 必需。規定要檢查的路徑。
options 可選。規定要返回的數組元素。預設是 all。

可能的值:

  • PATHINFO_DIRNAME - 只返回 dirname
  • PATHINFO_BASENAME - 只返回 basename
  • PATHINFO_EXTENSION - 只返回 extension
定義和用法

pathinfo() 函數以數組或字串的形式返回關於檔案路徑的資訊。

返回的數組元素如下:

  • [dirname]:返迴文件路徑中的目錄部分
  • [basename]:返迴文件路徑中檔案名稱的部分
  • [extension]:返迴文件路徑中檔案的類型的部分
提示和注釋

注釋:如果不是請求所有的元素,則 pathinfo() 函數返回字串。

php開啟pathinfo 路由模式:pathinfo 模式 需要 php.ini 開啟下面這個參數cgi.fix_pathinfo=1path_info模式:http://www.xxx.com/index.php/模組/方法
執行個體 1
<?php print_r(pathinfo("/testweb/test.txt")); ?> 

上面的代碼將輸出:

Array(    [dirname] => /testweb    [basename] => test.txt    [extension] => txt    [filename] => test)
執行個體 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)); ?> 

注意:PATHINFO_EXTENSION等不能加雙引號“”或單引號‘’。

警告:Warning: pathinfo() expects parameter 2 to be long, string given in C:\AppServ\www\test.php on line 3

上面的代碼將輸出:

string(8)"/testweb"string(8)"test.txt"string(3)"txt"
parse_url — 解析 URL,返回其組成部分
mixed parse_url ( string $url [, int $component = -1 ] )

本函數解析一個 URL 並返回一個關聯陣列,包含在 URL 中出現的各種組成部分。

本函數不是用來驗證給定 URL 的合法性的,只是將其分解為下面列出的部分。不完整的 URL 也被接受,parse_url()會嘗試盡量正確地將其解析。

參數

url:要解析的 URL。無效字元將使用 _ 來替換。

component:

指定 PHP_URL_SCHEME、 PHP_URL_HOST、 PHP_URL_PORT、 PHP_URL_USER、 PHP_URL_PASSPHP_URL_PATH、 PHP_URL_QUERY 或 PHP_URL_FRAGMENT 的其中一個來擷取 URL 中指定的部分的 string。 (除了指定為 PHP_URL_PORT 後,將返回一個 integer 的值)。

傳回值

對嚴重不合格的 URL,parse_url() 可能會返回 FALSE

如果省略了 component 參數,將返回一個關聯陣列 array,在目前至少會有一個元素在該數組中。數組中可能的鍵有以下幾種:

scheme - 如 httphostportuserpasspathquery - 在問號 ? 之後fragment - 在散列符號 # 之後

如果指定了 component 參數, parse_url() 返回一個 string (或在指定為 PHP_URL_PORT 時返回一個 integer)而不是 array。如果 URL 中指定的組成部分不存在,將會返回 NULL

執行個體分析:
<?phpprint_r (parse_url("https://www.so.com/s?q=strpos&src=360chrome_zoned"));

輸出結果:

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

執行個體2

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

運行結果:

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

Example #1 parse_url() 例子

<?php$url = ‘http://username:[email protected]/path?arg=value#anchor‘;print_r(parse_url($url));echo parse_url($url, PHP_URL_PATH);?>

以上常式會輸出:

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

Example #2 parse_url() 解析丟失協議的例子

<?php$url = ‘//www.example.com/path?googleguy=googley‘;// 在 5.4.7 之前這會輸出路徑 "//www.example.com/path"var_dump(parse_url($url));?>

以上常式會輸出:

array(3) {  ["host"]=>  string(15) "www.example.com"  ["path"]=>  string(5) "/path"  ["query"]=>  string(17) "googleguy=googley"}
注釋

Note:

本函數不能用於相對 URL。

Note:

parse_url() 是專門用來解析 URL 而不是 URI 的。不過為遵從 PHP 向後相容的需要有個例外,對 file:// 協議允許三個斜線(file:///...)。其它任何協議都不能這樣。

basename()—— 函數返迴路徑中的檔案名稱部分。
basename(path,suffix)
參數 描述
path 必需。規定要檢查的路徑。
suffix 可選。規定副檔名。如果檔案有 suffix,則不會輸出這個副檔名。

例子:

<?php$path = "/testweb/home.php";//顯示帶有副檔名的檔案名稱echo basename($path);//顯示不帶有副檔名的檔案名稱echo basename($path,".php");?> 

輸出:

home.phphome

http://php.net/manual/zh/function.parse-url.php

http://www.w3school.com.cn/php/func_filesystem_basename.asp

 

寫一個函數,儘可能高效的,從一個標準 url 裡取出檔案的副檔名?pathinfo檔案路徑& parse_url解析url & basename路徑中檔案名稱

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.