Posted By

localhorst on 03/03/08


Tagged

filename safe regexpr


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

adix
luman
lisandro


Filename safe


 / Published in: PHP
 

URL: http://www.phpit.net/code/filename-safe/

  1. <?php
  2. function filename_safe($filename) {
  3. $temp = $filename;
  4.  
  5. // Lower case
  6. $temp = strtolower($temp);
  7.  
  8. // Replace spaces with a '_'
  9. $temp = str_replace(" ", "_", $temp);
  10.  
  11. // Loop through string
  12. $result = '';
  13. for ($i=0; $i<strlen($temp); $i++) {
  14. if (preg_match('([0-9]|[a-z]|_)', $temp[$i])) {
  15. $result = $result . $temp[$i];
  16. }
  17. }
  18.  
  19. // Return filename
  20. return $result;
  21. }
  22.  
  23. function safe_filename ($filename) {
  24. $search = array(
  25. // Definition of German Umlauts START
  26. /ß/,
  27. /ä/,'/Ä/’,
  28. ‘/ö/’,'/Ö/,
  29. /ü/,'/Ü/’,
  30. // Definition of German Umlauts ENDE
  31. ‘([^[:alnum:]._])’ // Disallow: Not alphanumeric, dot or underscore
  32. );
  33. $replace = array(
  34. ’ss’,
  35. ‘ae’,'Ae’,
  36. ‘oe’,'Oe’,
  37. ‘ue’,'Ue’,
  38. _
  39. );
  40. return preg_replace($search,$replace,$filename);
  41. }
  42.  
  43.  
  44. ?>

Report this snippet  

You need to login to post a comment.