Collections functions
Encryption functions
General functions
Theme permission functions
Resource functions

mix_text()

Description

Utility function to randomly scramble string

Parameters

ColumnTypeDefaultDescription
$string string - Text string to scramble
$recurse boolean true - Optionally prevent recursion (maybe called by another mix unction)

Return

string

Location

include/migration_functions.php lines 335 to 452

Definition

 
function mix_text($string$recurse=true)
    {
    global 
$mixcache$mime_type_by_extension;
    if(isset(
$mixcache[md5($string)]))
        {
        return 
$mixcache[md5($string)];
        }
    
    
debug"Converting string<br/>" $string ", recurse=" . ($recurse "TRUE" "FALSE"));

    
// Check if another function is better
    
if(validateDatetime($string) && $recurse)
        {
        
debug("This is a date - calling mix_date()");
        return 
mix_date($string);
        }
    elseif(
strpos($string,"http") === 0  && $recurse)
        {
        
debug("This is a URL - calling mix_url()");
        return 
mix_url($string);
        }
    elseif(
in_array(mb_substr($string,strrpos($string,".")),$mime_type_by_extension) && $recurse)
        {
        
debug("This is a filename - calling mix_filename()");
        return 
mix_filename($string);
        }
    
    
$numbers '0123456789';
    
$uppercons 'BCDFGHJKLMNPQRSTVWXZ';
    
$uppervowels 'AEIOUY';
    
$lowercons 'bcdfghjklmnpqrstvwxz';
    
$lowervowels 'aeiouy';
    
$noreplace "'\".,<>#-_&\$£:;^?!@+()*% \n";

    
$newstring "";
    
$bytelength strlen($string);
    
$mbytelength mb_strlen($string);

    
// Simple conversion if numbers
    
if($bytelength == $mbytelength && (string)(int)$string == $string)
        {
        
$newstring =  mt_rand(0,(int)$string);
        }
    else
        {
        
// Process each character
        
for($i=0;$i<$mbytelength;$i++)
            {
            
$oldchar mb_substr($string,$i,1);
            
//debug("Converting character #$i '" . $oldchar . "'"); 

            
if($i && strpos($noreplace,$oldchar) === false)
                {
                
// Randomly add or remove character after first
                
$randaction mt_rand(0,10);
                if(
$randaction == 0)
                    {
                    
// Skip a character
                    
$i++;
                    }
                elseif(
$randaction == 1)
                    {
                    
// Add a character
                    
$i--;
                    }
                }
          
            if(
$i >= $mbytelength || $oldchar == "")
                {
                
$newstring .=  substr(str_shuffle($lowervowels $lowercons), 0,1);   
                }
            elseif(
strpos($noreplace,$oldchar) !== false)
                {
                
$newstring .= $oldchar;
                }
            elseif(
strlen($oldchar)==1)
                {
                
// Non- multibyte
                
if(strpos($lowercons,$oldchar) !== false)
                    {
                    
$newchar substr(str_shuffle($lowercons), 0,1);
                    }
                elseif(
strpos($uppercons,$oldchar) !== false)
                    {
                    
$newchar substr(str_shuffle($uppercons), 0,1);
                    }
                elseif(
strpos($lowervowels,$oldchar) !== false)
                    {
                    
$newchar substr(str_shuffle($lowervowels), 0,1);
                    }
                elseif(
strpos($uppervowels,$oldchar) !== false)
                    {
                    
$newchar substr(str_shuffle($uppervowels), 0,1);
                    }                    
                elseif(
strpos($numbers,$oldchar) !== false)
                    {
                    
$newchar substr(str_shuffle($numbers), 0,1);
                    }
                else
                    {
                    
$newchar substr(str_shuffle($noreplace), 0,1);
                    }
                
//debug("New random character: $newchar");
                
$newstring .= $newchar;        
                }                         
            else
                {
                
$newchar random_char();
                
$newstring .= $newchar;   
                
//debug("New random character: " . $newchar);
                
// End of multibyte conversion
            
}
        }

    
// Update cache
    
$mixcache[md5($string)] = $newstring;
    return 
$newstring;
    }

This article was last updated 10th July 2020 19:05 Europe/London time based on the source file dated 24th June 2020 15:23 Europe/London time.