PHP code example of magebitcom / pii-redactor

1. Go to this page and download the library: Download magebitcom/pii-redactor 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/ */

    

magebitcom / pii-redactor example snippets


use Magebit\PiiRedactor\PiiRedactor;

$redactor = new PiiRedactor();

echo $redactor->redact('Mail [email protected], card 4111 1111 1111 1111')->text();
// Mail [EMAIL], card [CREDIT_CARD]

foreach ($redactor->analyze($text)->matches() as $match) {
    printf("%s [%d, %d) score %.2f via %s\n",
        $match->entityType, $match->start, $match->end, $match->score, $match->detectorName);
}

use Magebit\PiiRedactor\Context\ContextWords;
use Magebit\PiiRedactor\Detector\DetectorRegistry;
use Magebit\PiiRedactor\EntityType;

// English context words only (default)
$redactor = new PiiRedactor();

// Extend an existing entity type (e.g. Latvian phone words) and add a custom one
$words = ContextWords::default()
    ->withWords(EntityType::PHONE, ['tālrunis', 'telefons'])   // built-in type
    ->withWords('EMPLOYEE_ID', ['employee', 'staff']);          // custom type (string)

$redactor = new PiiRedactor(DetectorRegistry::withDefaults($words));

use Magebit\PiiRedactor\Analyzer;
use Magebit\PiiRedactor\NullContextEnhancer;
use Magebit\PiiRedactor\Detector\DetectorRegistry;

$analyzer = new Analyzer(DetectorRegistry::withDefaults(), new NullContextEnhancer());

use Magebit\PiiRedactor\RedactionConfig;
use Magebit\PiiRedactor\Strategy\MaskStrategy;
use Magebit\PiiRedactor\Strategy\ReplaceStrategy;

$config = new RedactionConfig(
    ['CREDIT_CARD' => new MaskStrategy('*', 4, true)],   // **** -> ************1111
    new ReplaceStrategy('<REDACTED:{type}>'),            // default for everything else
);

$redactor = new PiiRedactor(config: $config);

use Magebit\PiiRedactor\AnalyzerOptions;

$options = new AnalyzerOptions(
    entityTypes: ['EMAIL', 'CREDIT_CARD'],   // restrict types
    minScore: 0.6,                           // raise threshold
    allowList: ['[email protected]'],      // never redact these literals
);

use Magebit\PiiRedactor\Detector\DetectorRegistry;
use Magebit\PiiRedactor\Detector\Pattern;
use Magebit\PiiRedactor\Detector\PatternDetector;

$registry = DetectorRegistry::withDefaults();
$registry->register(new PatternDetector('employee-id', 'EMPLOYEE_ID', [
    new Pattern('emp', '/\bEMP-\d{6}\b/u', 0.9, ['EMP-']),
]));

$redactor = new PiiRedactor($registry);