Skip to content

Instantly share code, notes, and snippets.

@gskema
Created April 24, 2017 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gskema/0e4025bd1be54b13858dff90589c0d3a to your computer and use it in GitHub Desktop.
Save gskema/0e4025bd1be54b13858dff90589c0d3a to your computer and use it in GitHub Desktop.
Obscure arbitrary security-sensitive string in PHP
<?php
/**
* Obscures sensitive string (like a security token),
* but preserves minimal information about letters, digits, spaces and unknown characters.
*
* @param string $string
*
* @return string
*/
protected function obscure(string $string)
{
// Input: '_ ?$$EFdasdasda das56 14a6512341 2341 )_% UIML: '
// Output: '?_???aaaaaaaaaa_aaa00_00a0000000_0000_???_aaaa?_'
$obscured = preg_replace('/[^a-z\s\d]/i', '?', $string);
$obscured = preg_replace('/[a-z]/i', 'a', $obscured);
$obscured = preg_replace('/\d/', '0', $obscured);
$obscured = preg_replace('/\s/', '_', $obscured);
return $obscured;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment