PHP code example of hashyoo / sensitive-words

1. Go to this page and download the library: Download hashyoo/sensitive-words library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hashyoo / sensitive-words example snippets


$sensitive_words = array(
  'SB', '傻逼', 'Fuck'
);


use HashyooWordsSafe\Facade\WordsSafe;

$text = 'SB就是傻逼!fuck is a bad word!';

WordsSafe::load($sensitive_words);
$result = WordsSafe::escape($text); // escape 方法会将文本中找到的敏感词使用替代词(默认是*)替换掉。
echo $result; // **就是**!**** is a bad word!

// 你也可以改变默认的替换字符,如换成 'x'。
WordsSafe::setEscapeChar('x');
echo WordsSafe::escape($text); // xx就是xx!xxxx is a bad word!



use HashyooWordsSafe\Facade\WordsSafe;

$username = $_POST['username'];

$sensitive_words = ['Singiu'];
WordsSafe::load($sensitive_words);
$bad_words = WordsSafe::scan($username);

if (count($bad_words) > 0) {
  echo '昵称中含有不合适的字符!';
}