0

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>".

Jacob Grahn
  • 111
  • 1
  • 7

1 Answers1

0

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"
    ?>
BigBen_Davy
  • 17
  • 1
  • 10
  • You basically make an array of unwanted chars, and then use the str_replace function to erase them. if you wish to check an html form that is involved with a textarea or input value, make $userInput = – BigBen_Davy Aug 30 '16 at 19:04