php 副檔名擷取方法匯總

來源:互聯網
上載者:User
  1. //取檔案的副檔名

  2. //by http://bbs.it-home.org
  3. $file = "/home/jbxue/file_20130322.txt";

  4. for($i=1; $i < 6; $i++) {

  5. $func = 'get_file_ext_' . $i;
  6. var_dump($func($file));
  7. }

  8. function get_file_ext_1($file) {

  9. return strtolower(trim(substr(strrchr($file, '.'), 1)));
  10. }

  11. function get_file_ext_2($file) {

  12. return strtolower(trim(pathinfo($file, PATHINFO_EXTENSION)));
  13. }

  14. function get_file_ext_3($file) {

  15. return strtolower(trim(substr($file, strrpos($file, '.')+1)));
  16. }

  17. function get_file_ext_4($file) {

  18. return strtolower(trim(array_pop(explode('.', $file))));
  19. }

  20. function get_file_ext_5($file) {

  21. $tok = strtok($file, '.');
  22. while($tok !== false) {
  23. $return = $tok;
  24. $tok = strtok('.');
  25. }
  26. return strtolower(trim($return));
  27. }
  28. ?>

複製代碼

附:php 副檔名小知識副檔名是作業系統用來標誌檔案格式的一種機制。通常來說,一個副檔名是跟在主檔案名後面的,由一個分隔字元分隔。在一個像“readme.txt”的檔案名稱中,readme是主檔案名,txt為副檔名,表示這個檔案被認為是一個純文字檔案。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.