<?php
 
class StringObscure
 
{
 
    /**
 
     * Obscure String
 
     * Takes a string and changes some characters to obscure it
 
     * @param string $string // The string to obscure
 
     * @param int $num_to_obscure The number of characters in the string to obscure
 
     * @param string $obscure_char The string to obscure with (can be more than one character long)
 
     */
 
    function obscure($string, $num_to_obscure = -1, $obscure_char = '*')
 
    {
 
        if(empty($obscure_char)) // Prevent $obscure_char from being empty
 
             $obscure_char = '*';
 
 
        if($num_to_obscure == -1) { // If there is no input for the amount of characters to obscure
 
            $num_to_obscure = round(strlen($string) / 2); // Obscure half of the characters
 
        }
 
        
 
        if($num_to_obscure > strlen($string)) { // Make sure that the number to obscure is no greater than the length of the string
 
            $num_to_obscure = strlen($string);
 
        }
 
         
 
        $string = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY); // Split string into array
 
        $obscured = array();
 
         
 
        for($i = 0; $i < $num_to_obscure; $i++) { // Obscure characters
 
            do {
 
                $rand = rand(0, count($string));
 
            } while (array_key_exists($rand, $obscured));
 
             
 
            $string[$rand] = $obscure_char; // Obscure specific character
 
            $obscured[$rand] = true; // Create key
 
        }
 
         
 
        return implode('', $string);
 
    }
 
    
 
    /**
 
     * Obscure String w/Percentage
 
     * Takes a string and changes some characters to obscure it using a percentage of the string length
 
     * @param string $string // The string to obscure
 
     * @param int $pct_to_obscure The percentage of characters in the string to obscure
 
     * @param string $obscure_char The string to obscure with (can be more than one character long)
 
     */
 
    function obscurePercentage($string, $pct_to_obscure = -1, $obscure_char = '*')
 
    {
 
        if(empty($obscure_char)) // Prevent $obscure_char from being empty
 
             $obscure_char = '*';
 
 
        if($pct_to_obscure == -1) { // If there is no input for the amount of characters to obscure
 
            $num_to_obscure = round(strlen($string) / 2); // Obscure half of the characters
 
        } else {
 
            if($pct_to_obscure > 100) { // Make sure that the percentage is 100% or less
 
                $pct_to_obscure = 100;
 
            } else {
 
                if($pct_to_obscure < 0) { // Make sure that the percentage is 0% or greater
 
                    $pct_to_obscure = 0;
 
                }
 
            }
 
            $pct_to_obscure = $pct_to_obscure / 100; // Turn the percentage into a decimal
 
            $num_to_obscure = round(strlen($string) * $pct_to_obscure); // Calculate the number of characters to be obscured
 
        }
 
         
 
        $string = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY); // Split string into array
 
        $obscured = array();
 
         
 
        for($i = 0; $i < $num_to_obscure; $i++) { // Obscure characters
 
            do {
 
                $rand = rand(0, count($string));
 
            } while (array_key_exists($rand, $obscured));
 
             
 
            $string[$rand] = $obscure_char; // Obscure specific character
 
            $obscured[$rand] = true; // Create key
 
        }
 
         
 
        return implode('', $string);
 
    }
 
}
 
 
// Examples
 
 
 
$str = 'Random String to be obscured...';
 
 
echo 'String before obscuring: ' . $str; // Outputs string, unaltered
 
echo 'String after obscuring: ' . stringObscure::obscure($str); // Outputs string with half of the characters obscured
 
echo 'String after obscuring: ' . stringObscure::obscure($str, 10); // Outputs string with ten of the characters obscured
 
echo 'String after obscuring: ' . stringObscure::obscure($str, 7, '-'); // Outputs string with seven of the characters changed to -
 
echo 'String after obscuring: ' . stringObscure::obscurePercentage($str, 25); // Outputs string with 25 percent of the characters obscured
 
 |