Diese PHP Funktion ist sehr hilfreich wenn man Eingabefelder/Textbereiche hat dessen Text in eine DB oder einfach nur in eine Datei abgespeichert werden soll.

  1. /**
  2.  * clean_text
  3.  *
  4.  * diese function wandelt alle Sonderzeichen eines textes die falsch
  5.  * interpretiert werden koennen und die hltml tags um.
  6.  * also wenn man einen text von einer texbox oder input in eine DB
  7.  * abspeichern will.
  8.  * dann steht der text sauber in der DB drinn.
  9.  * ohne das falsche Formatierungen dabei heraus kommen.
  10.  *
  11.  * @return formatet string, special chars are fomrated into &#
  12.  * @param text as string $text
  13.  * @desc replaces all special chares into html entities
  14.  * @author banana mail@bananas-playground.net
  15. */
  16. function clean_text($text)
  17. {
  18. if($text == "")
  19. {
  20. return "";
  21. }
  22. else
  23. {
  24. $text = trim($text);
  25. $text = stripslashes($text);
  26.  
  27. // prevents adding everytime you change text a new <br />
  28. $text = preg_replace("/<br \/>/","",$text);
  29. //Remove sneaky spaces
  30. $text = str_replace( chr(0xCA),"",$text );
  31. $text = str_replace( "&","&",$text );
  32. $text = str_replace( "<!--","&#60;&#33;--",$text );
  33. $text = str_replace( "-->","--&#62;",$text );
  34. $text = preg_replace( "/<script/i","&#60;script",$text );
  35. $text = htmlentities($text);
  36. $text = str_replace( ">",">",$text );
  37. $text = str_replace( "<","<",$text );
  38. $text = str_replace( "\"",""",$text );
  39. // Convert literal newlines
  40. $text = preg_replace( "/\n/","<br />",$text );
  41. $text = preg_replace( "/\\\$/","$",$text );
  42. $text = str_replace( "!","!",$text );
  43. // IMPORTANT: It helps to increase sql query safety.
  44. $text = str_replace( "'","'",$text );
  45. $text = preg_replace("/&#([0-9]+);/s", "&#\\1;", $text);
  46.  
  47. return $text;
  48. }
  49. }