Search by

phil / watched-words

phil

Keep slurs, spam and banned phrases out of posts: watched words that block, hold, flag, censor, replace, link or tag, seeing through leetspeak.

0.1.0-alpha 2026-11-29 22:00 UTC

This package is auto-updated.

Last update: 2026-11-30 02:53:56 UTC


README

Keep slurs, spam and banned phrases out of posts: watched words that block, hold, flag, censor, replace, link or tag, seeing through leetspeak.

phil/watched-words is a plain PHP library. A site keeps a list of watched words — each with what to do when it appears — and scans every post, comment, message or profile against it before publishing. The result says whether the text is published, held for a moderator or refused, which tags it receives, and how its words are rewritten.

  • Seven actions: block the text, hold it for approval, flag it to the moderators, censor the words, replace them, turn them into a link, or tag the text. One list mixes them freely; a block wins over a hold, a hold over publishing.
  • Whole words by default: ass never matches inside class or assassin. A * stands for any letters inside a word, so *spam* catches spammers; a phrase matches across spaces and punctuation, and a lone * stands for any one word of a phrase.
  • Sees through evasion: case, accents, fullwidth and mathematical letters, invisible zero-width characters, leetspeak ($h!7), stretched letters (fuuuck, while cool still needs its two o) and Cyrillic or Greek letters drawn like Latin ones. Each trick can be turned off.
  • Allowed words: scunthorpe allowed keeps a *cunt* entry away from the town; a hit that lies inside an allowed word or phrase is dropped.
  • Case when it matters: an entry can compare the word as typed, so NASA is caught while nasa is not.
  • Regular expressions included: an entry can be a PCRE expression, checked when it is saved; an expression that gives up on a crafted text holds the text for a moderator instead of letting it through.
  • A test button: Scanner::test() tries a draft entry on a sample text and shows what it would catch.
  • Safe rewriting: censored, replaced and linked words come back as segments of plain text and normalised http or https links, for the renderer to escape — never as markup.
  • Bounded and fast: words are looked up by their folded letters, so a post of a few hundred words scans in about half a millisecond against a thousand entries; texts, lists, patterns and hits all have limits. The scanner keeps no state between scans, safe in a long-running worker.
  • Versioned JSON: a list encodes to a small JSON object with its version, for a settings column or an export; decoding checks every field, within a size limit.

Status

Part State
Entries: words, phrases, wildcards, regular expressions, case Implemented and tested
The seven actions, the outcome of a text and its tags Implemented and tested
Evasion: case, accents, compatibility forms, invisible characters, leetspeak, repeats, lookalikes Implemented and tested
Allowed words Implemented and tested
Rewriting into plain and link segments Implemented and tested
The JSON codec of watch lists Implemented and tested, fuzzed

Documentation

Document What it covers
Installation Requirements, installing, a first scan.
Patterns Words, phrases, wildcards, regular expressions, case, and allowed words.
Evasion How words are folded before they are compared, and what is not seen through.
Actions and outcomes What each action does, the outcome of a text, rewriting and its segments.
Storage The JSON codec and the columns of an entry.
Extension points What an application may replace or tune, and what is stable.
Testing The tests, the fuzz target and the benchmark.
Reference Every public type, with its role.

Quick start

composer require phil/watched-words
use Phil\WatchedWords\Action;
use Phil\WatchedWords\Outcome;
use Phil\WatchedWords\Scanner;
use Phil\WatchedWords\WatchedWord;
use Phil\WatchedWords\WatchList;

$list = new WatchList([
    new WatchedWord('12', 'fuck', Action::Block),
    new WatchedWord('13', '*cunt*', Action::Censor),
    new WatchedWord('14', 'free crypto', Action::Hold),
    new WatchedWord('15', 'faq', Action::Link, 'https://example.com/faq'),
    new WatchedWord('16', 'spoiler*', Action::Tag, 'spoilers'),
], allowed: ['scunthorpe']);

$scanner = new Scanner($list);           // compile once, scan every text
$result = $scanner->scan($post->body);

match ($result->outcome()) {
    Outcome::Block => $form->addError('Please rephrase: '.$result->hitsFor(Action::Block)[0]->text),
    Outcome::Hold => $queue->holdForApproval($post->id),
    Outcome::Publish => $post->publish($result->segments(), $result->tags()),
};

Requirements

PHP 8.5 or later with ext-mbstring; phil/url for link addresses, and symfony/polyfill-intl-normalizer where ext-intl is missing.

Contributing

Contributions are welcome on the PHIL monorepo: read the contribution guide and the code of conduct. Report a vulnerability privately, as the security policy explains, never in a public issue.

License

MIT, © Philippe Gamache, doing business as Le Gnome Archiviste; a Parler Haut Interagir Librement project. See LICENSE.