I've been trying and searching for a working solution, but nothing seemed to work..
I'm making a social website, where the users can post. I would like to check the text before it goes into the database.
I would like to make the regular characters enabled (a-zA-Z0-9) + some utf8 characters (áÁéÉíÍóÓöÖőŐúÚüÜűŰ) and some other characters (.,?!;. etc.)
I've been trying, but it doesn't seem to work:
preg_replace('/[^a-zA-Z0-9áÁéÉíÍóÓöÖőŐúÚüÜűŰ \.\,\?\!\:\;\-\+\<\>\%\~\€\$\[\]\{\}\@\&\#\*\"\„\”\/]/', '', $string);
Could someone help me out?
Is there anything easier? The reason why I'm doing this is because I don't wanna allow the users to spam the database with illegal characters, or HTML tags.. It would be so bad if someone wrote a post like this "blabla</div>".
If you are using a textarea or input value to record the User entries, you could use a str_replace() function to search through the string of characters to replace the illegal characters. Link to str_replace in php manual: http://php.net/manual/en/function.str-replace.php
Code example:
<?php
$userInput = "Tex.t tha!t wil;l be te>sted that is inp?uted b;y user";
//Following Array is all characters you deem unwanted
$illegalChar = array(".", ",", "?", "!", ":", ";", "-", "+", "<", ">", "%", "~", "€", "$", "[", "]", "{", "}", "@", "&", "#", "*", "„");
$charString = str_replace($illegalChar, "", $userInput);
echo $charString;
//Output is "Text that will be tested that is inputed by user"
?>