PHP code example of gbhorwood / redactem

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

    

gbhorwood / redactem example snippets




use Gbhorwood\Redactem\Redact;

$redactedJson = \Gbhorwood\Redactem\Redact::passwords($originalJson);

$redactedJson = \Gbhorwood\Redactem\Redact::passwords($originalJson, 'REDACTED');

$redactedJson = \Gbhorwood\Redactem\Redact::creditcards($originalJson);

$redactedJson = \Gbhorwood\Redactem\Redact::creditcards($originalJson, 'REDACTED');

$redactedJson = \Gbhorwood\Redactem\Redact::emails($originalJson);

$redactedJson = \Gbhorwood\Redactem\Redact::emails($originalJson, 'REDACTED');

$redactedJson = \Gbhorwood\Redactem\Redact::byKey($originalJson, 'somekey');

$redactedJson = \Gbhorwood\Redactem\Redact::byKey($originalJson, 'SomeKey', true);

$redactedJson = \Gbhorwood\Redactem\Redact::byKey($originalJson, 'somekey', true, 'REDACTED');

$redactedJson = \Gbhorwood\Redactem\Redact::byKeys($originalJson, ['somekey', 'otherkey'], true, 'REDACTED');

$redactedJson = \Gbhorwood\Redactem\Redact::byRegex($originalJson, '/someregex/');

$redactedJson = \Gbhorwood\Redactem\Redact::byRegex($originalJson, '/someregex/', 'REDACTED');

/**
 * A function to test if a key/value pair should be redacted
 * Here, if the key is equal to 'secret' and the value is only digits.
 * @param  String|Int $k The key of a key/value pair
 * @param  String|Int $v The value of a key/value pair
 * @return bool
 */
$shouldRedact = fn ($k, $v) => (bool)($k == 'secret' && preg_match('/^[0-9]*$/', (string)$v));

/**
 * A function to build the redaction text for a value
 * Here, a string of exclamation marks the length of the string being redacted
 * @param  String|Int $v The value of a key/value pair
 * @return String
 */
$redactionText = fn ($v) => join(array_fill(0, strlen((string)$v), '!'));

$redactedJson = Redact::redact($originalJson, $shouldRedact, $redactionText);