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.
/** * clean_text * * diese function wandelt alle Sonderzeichen eines textes die falsch * interpretiert werden koennen und die hltml tags um. * also wenn man einen text von einer texbox oder input in eine DB * abspeichern will. * dann steht der text sauber in der DB drinn. * ohne das falsche Formatierungen dabei heraus kommen. * * @return formatet string, special chars are fomrated into &# * @param text as string $text * @desc replaces all special chares into html entities * @author banana mail@bananas-playground.net */ function clean_text($text) { if($text == "") { return ""; } else { // prevents adding everytime you change text a new <br /> //Remove sneaky spaces // Convert literal newlines $text = preg_replace( "/\n/","<br />",$text ); $text = preg_replace( "/\\\$/","$",$text ); $text = str_replace( "!","!",$text ); // IMPORTANT: It helps to increase sql query safety. $text = str_replace( "'","'",$text ); $text = preg_replace("/&#([0-9]+);/s", "&#\\1;", $text); return $text; } }
