Detecting specific words in a textarea submission

前端 未结 3 1089
深忆病人
深忆病人 2021-01-26 07:07

I have a new feature on my site, where users can submit any text (I stopped all HTML entries) via a textarea. The main problem I still have though is that they could type \"htt

3条回答
  •  情书的邮戳
    2021-01-26 07:25

    A simple way to do this is to put all the words not allowed into an array and loop through them to check each one.

    $banned = array('http://', '.com', '.net', 'www.', '.org'); // Add more
    foreach ($banned as $word):
        if (strpos($entry, $word) !== false) die('Contains banned word');
    endforeach;
    

    The problem with this is if you get too carried away and start banning the word 'com' or something, there are other words and phrases that could be perfectly legal that contains the letters 'com' in that way that would cause a false positive. You could use regular expressions to search for strings that look like URLs, but then you can easily just break them up like I did above. There is no effective way to completely stop people from posting links into a comment. If you don't want them there, you'll ultimately just have to use moderation. Community moderation works very well, look at Stack Overflow for instance.

提交回复
热议问题