Remove special characters that mess with formating

后端 未结 2 1912
囚心锁ツ
囚心锁ツ 2021-01-26 21:45

I am currently creating a chat and can\'t seem to find a way to stop users from posting special characters that mess with formatting of the chat and lagging end users out of the

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-26 21:56

    If you're looking for a quick fix, I would use a regex like this:

    $cleanMessage = preg_replace("/[^\x20-\xAD\x7F]/", "", $input_lines);
    

    Or, if you prefer:

    $cleanMessage = preg_filter("/[\x20-\xAD\x7F]/", "", $input_lines);
    

    Both of these are identical in functionality. It's up to you which one you want to use.

    These remove all characters outside of extended ASCII. This means that "normal" text and the most commonly accented Roman characters will still work, but "zalgo" style text will not. Unfortunately, the side effect is that Arabic, Japanese, Chinese, Cyrillic, etc. will also be stripped as "bad".

    There's no trivial way to just prevent the kind of abuse you're seeing, because there are so many Unicode tricks you can use to apply diacritic marks to letters. It'd be a full-time job to attempt to filter them out in a way that didn't affect some language somewhere.

    My non-technical advice would be to allow users to report people who post these kinds of messages, so that they can be banned by an administrator.

提交回复
热议问题