Privacy Filter: Detect PII in Text from Laravel

Published on by

Privacy Filter: Detect PII in Text from Laravel image

Privacy Filter is a Laravel package that detects private entities — names, emails, and other personally identifiable information — in arbitrary text. It wraps the privacy-filter.cpp command-line binary, a GGML inference engine for OpenAI's privacy-filter token-classification models, and exposes the results through a PHP API.

Classifying Text into Entities

The main entry point is the entities() method, which runs the model over a string and returns Entity instances. Each entity carries its type, the matched text, the start and end byte offsets, and a confidence score:

use DirectoryTree\PrivacyFilter\Facades\PrivacyFilter;
 
$entities = PrivacyFilter::entities('Contact John Doe at jdoe@example.com.');
 
foreach ($entities as $entity) {
echo $entity->type; // private_email
echo $entity->text; // jdoe@example.com
}

Because the offsets are exact UTF-8 byte positions, you can map each detected entity back to its location in the original text rather than relying on string matching alone.

Confidence Thresholds

Classifications default to a 0.5 confidence threshold. You can raise or lower it per call to trade recall for precision — a higher threshold returns fewer, more certain entities:

$entities = PrivacyFilter::entities(
text: 'Contact John Doe at jdoe@example.com.',
threshold: 0.75,
);

Redacting Detected Text

The package detects entities; what you do with them is up to you. A redaction pass is a reduce over the returned entities:

$redacted = collect($entities)->reduce(function (string $text, $entity) {
return str_replace($entity->text, '[redacted]', $text);
}, $text);

Faking Classifications in Tests

Running the binary and loading the model in a test suite is slow, so Privacy Filter ships a fake() method that returns predetermined entities without invoking the binary:

use DirectoryTree\PrivacyFilter\Entity;
use DirectoryTree\PrivacyFilter\Facades\PrivacyFilter;
 
PrivacyFilter::fake([
new Entity(type: 'private_email', start: 20, end: 36, score: 0.98),
]);

Your code under test then receives those entities as if the model had produced them.

Installation and Setup

Install the package with Composer, then run the installer to fetch the compiled binary for your operating system and download the required GGUF model:

composer require directorytree/privacy-filter
php artisan privacy-filter:install

Prebuilt binaries for Linux, macOS (including ARM64), and Windows are distributed through PrivacyFilterBinaries. Pass --force to overwrite existing files. To customize the binary path, model path, process timeout, model URL, or release source, publish the configuration file:

php artisan vendor:publish --tag=privacy-filter-config

Every classification loads the model into memory, so the package recommends running classifications on dedicated queue workers in production to keep memory usage predictable.

You can find the source and documentation on GitHub.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum

The latest

View all →
Showcase Your PhpStorm Expertise on LinkedIn image

Showcase Your PhpStorm Expertise on LinkedIn

Read article
NationForge: A Self-Hosted Admin Panel for Civic Organizations image

NationForge: A Self-Hosted Admin Panel for Civic Organizations

Read article
Monitor Laravel Queues, Commands, and Schedulers on Any Driver with Vigilance image

Monitor Laravel Queues, Commands, and Schedulers on Any Driver with Vigilance

Read article
Watch the Teaser for 'The Story of PHP' Documentary image

Watch the Teaser for 'The Story of PHP' Documentary

Read article
Ship AI with Laravel: Give Your AI Agent Live Web Search image

Ship AI with Laravel: Give Your AI Agent Live Web Search

Read article
Lattice: Describe Inertia UIs in PHP image

Lattice: Describe Inertia UIs in PHP

Read article