Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

mix_text()

Description

Utility function to randomly scramble string

Parameters

ColumnTypeDefaultDescription
$string null|string Text string to scramble
$recurse boolean true: string { global $mixcache Optionally prevent recursion (maybe called by another mix unction)
$mime_type_by_extension;

Return

string

Location

include/migration_functions.php lines 287 to 408

Definition

 
function mix_text(?string $stringbool $recurse=true): string
    
{
    global 
$mixcache$mime_type_by_extension;

    
$string ??= '';
    if (
trim($string) === '')
        {
        return 
'';
        }

    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);

            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);
                    }
                
$newstring .= $newchar;        
                }                         
            else
                {
                
$newchar random_char();
                
$newstring .= $newchar;   
                } 
// End of multibyte conversion
            
}
        }

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

This article was last updated 19th March 2024 11:35 Europe/London time based on the source file dated 26th February 2024 11:00 Europe/London time.