一款实用php 正则文件扩展名代码

  1. */
  2. function attachicon($type,$size='') {
  3. static $attachicons = array(
  4. 0 => 'common.gif',
  5. 1 => 'image.gif',
  6. 2 => 'binary.gif',
  7. 3 => 'rar.gif',
  8. 4 => 'msoffice.gif',
  9. 5 => 'text.gif',
  10. 6 => 'html.gif',
  11. 7 => 'real.gif',
  12. 8 => 'av.gif',
  13. 9 => 'flash.gif',
  14. 10 => 'pdf.gif',
  15. 11 => 'torrent.gif'
  16. );
  17. if(preg_match("/image|^(jpg|gif|png|bmp)/", $type)) {
  18. $typeid = 1;
  19. } elseif(preg_match("/bittorrent|^torrent/", $type)) {
  20. $typeid = 11;
  21. } elseif(preg_match("/pdf|^pdf/", $type)) {
  22. $typeid = 10;
  23. } elseif(preg_match("/flash|^(swf|fla|swi)/", $type)) {
  24. $typeid = 9;
  25. } elseif(preg_match("/audio|video|^(wav|mid|mp3|m3u|wma|asf|asx|vqf|mpg|mpeg|avi|wmv)/", $type)) {
  26. $typeid = 8;
  27. } elseif(preg_match("/real|^(rm|rv)/", $type)) {
  28. $typeid = 7;
  29. } elseif(preg_match("/htm|^(php|js|pl|cgi|asp教程)/", $type)) {
  30. $typeid = 6;
  31. } elseif(preg_match("/text|^(txt|rtf|wri|chm)/", $type)) {
  32. $typeid = 5;
  33. } elseif(preg_match("/word|powerpoint|^(doc|ppt)/", $type)) {
  34. $typeid = 4;
  35. } elseif(preg_match("/compressed|^(zip|arj|rar|arc|cab|lzh|lha|tar|gz)/", $type)) {
  36. $typeid = 3;
  37. } elseif(preg_match("/octet-stream|^(exe|com|bat|dll)/", $type)) {
  38. $typeid = 2;
  39. } else {
  40. $typeid = 0;
  41. }
  42. return $size.$attachicons[$typeid];
  43. }