Cleaning a string


/ Published in: PHP
Save to your folder(s)

Wandelt eine Zeichenkette, die Sonderzeichen enthält, in eine Zeichenkette ohne Sonderzeichen.

Converts a string, the special characters contains in a string without special characters.


Copy this code and paste it in your HTML
  1. function CleaningAString($string)
  2. {
  3. //$string = strtolower($string);
  4.  
  5. // Fix german special chars
  6. $string = preg_replace('/[äÄ]/', 'ae', $string);
  7. $string = preg_replace('/[üÜ]/', 'ue', $string);
  8. $string = preg_replace('/[öÖ]/', 'oe', $string);
  9. $string = preg_replace('/[ß]/', 'ss', $string);
  10.  
  11. // Replace other special chars
  12. $specialCharacters = array(
  13. '#' => 'sharp',
  14. '$' => 'dollar',
  15. '%' => 'prozent', //'percent',
  16. '&' => 'und', //'and',
  17. '@' => 'at',
  18. '.' => 'punkt', //'dot',
  19. '€' => 'euro',
  20. '+' => 'plus',
  21. '=' => 'gleich', //'equals',
  22. '§' => 'paragraph',
  23. );
  24.  
  25. while (list($character, $replacement) = each($specialCharacters)) {
  26. $string = str_replace($character, '-' . $replacement . '-', $string);
  27. }
  28.  
  29. $string = strtr($string,
  30. "ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ",
  31. "AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn"
  32. );
  33.  
  34. // Remove all remaining other unknown characters
  35. $string = preg_replace('/[^a-zA-Z0-9\-]/', '-', $string);
  36. $string = preg_replace('/^[\-]+/', '', $string);
  37. $string = preg_replace('/[\-]+$/', '', $string);
  38. $string = preg_replace('/[\-]{2,}/', '-', $string);
  39.  
  40. return $string;
  41. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.