Posted By

fackz on 08/10/09


Tagged

clean special characters


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

fackz
irishsk
umang_nine


Clean Special Characters


 / Published in: PHP
 

URL: http://www.bala-krishna.com/how-to-clean-special-characters-from-php-string

  1. <?php
  2.  
  3. function just_clean($string)
  4. {
  5. // Replace other special chars
  6. $specialCharacters = array(
  7. '#' => '',
  8. '$' => '',
  9. '%' => '',
  10. '&' => '',
  11. '@' => '',
  12. '.' => '',
  13. '€' => '',
  14. '+' => '',
  15. '=' => '',
  16. '§' => '',
  17. '\\' => '',
  18. '/' => '',
  19. );</code>
  20.  
  21. while (list($character, $replacement) = each($specialCharacters)) {
  22. $string = str_replace($character, '-' . $replacement . '-', $string);
  23. }
  24.  
  25. $string = strtr($string,
  26. "ÀÁÂÃÄÅ� áâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ",
  27. "AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn"
  28. );
  29.  
  30. // Remove all remaining other unknown characters
  31. $string = preg_replace('/[^a-zA-Z0-9\-]/', ' ', $string);
  32. $string = preg_replace('/^[\-]+/', '', $string);
  33. $string = preg_replace('/[\-]+$/', '', $string);
  34. $string = preg_replace('/[\-]{2,}/', ' ', $string);
  35.  
  36. return $string;
  37. }
  38.  
  39.  
  40. ?>

Report this snippet  

You need to login to post a comment.