You get a bonus - 1 coin for daily activity. Now you have 1 coin

How to check all user messages for dangerous characters before saving?

Practice



How to check all user messages for dangerous characters before saving?
Need to strip all HTML tags
remove SQL operations
break a long word into shorter words so they fit within the length
remove certain characters from the message

PHP:
function Filtr_Message($text,$length)
{



$text=substr($text, 0, $length);

$text = preg_replace ("/[^a-zA-ZА-Яа-я0-9 @.,-=+\s]/u","",$text);
$text=preg_replace('/([^\s]{40})/' , "$1 ", $text);

$text = trim( strip_tags( $text ) );
$text = htmlspecialchars($text);
$text = mysql_escape_string($text);
$quotes = array ("\x27", "\x22", "\x60", "\t", "\n", "\r", "*", "%", "<", ">", "?", "!" );
$goodquotes = array ("-", "+", "#" );
$repquotes = array ("\-", "\+", "\#" );

$text = str_replace( $quotes, '', $text );
$text = str_replace( $goodquotes, $repquotes, $text );
$text = ereg_replace(" +", " ", $text);
return $text;

}




note that for preg_replace to support UTF-8 encoding, you need to add /u at the end of the pattern

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Running server side scripts using PHP as an example (LAMP)"

Terms: Running server side scripts using PHP as an example (LAMP)