Jump to content

Is it possible to display an obscured value from a mysql database?


jdock1

Recommended Posts

I want to make a "hint" feature for an app ive been working on. Its a guessing game, and I want to be able to give out hints.

 

Ok so say there is an answer in the database. Say the value is "firetruck". I want to retrieve that value, and obscure it, displaying only one or two letters that are in the word.

 

Or, for a number, say 178, I want to display only one digit from it.

 

Is this possible? Im sure it is, anything seems possible with PHP & Mysql. If so, how could I implement this?

 

Trust me, I read the manuals. The manuals for both PHP and Mysql are so vast and a little advanced for my level.. then again im not complete novice and know the basics plus more of both.

 

Link to comment
Share on other sites

One of the other of these is what you need.

PHP: substr

MySQL: SUBSTRING()

Thanks! Thats what I was looking for. Im trying to use the php substr, but nothing is being echod. Idk. I probablyneed to use mysql substring, havent looked at it yet. But heres my code:

 

<?php
include_once('admin/includes/linkmysql.php');
$query= "SELECT * FROM userinfo WHERE USERNAME = '$uname'";
$result= mysql_query($query,$link);
$fetchdata= mysql_fetch_array($result);
$num= $fetchdata['fixednum'];

?>
<fieldset><legend><b>Get Hint</b></legend>
<form action="" method="post" />
<br>
<br>
<input type="submit" name="RequestSubstr" value="Get Hint" />
<?php
if ($_POST['RequestSubstr'] == "Get Hint")
{
echo $num[strlen($num)-1];
}
?>

Link to comment
Share on other sites

Try this out. I just wrote it up without testing it, so it may have some minor errors, but the idea is what you're after.

<?php
/**
* Obscure characters in a string
*
* @param $string The string to be obscured
* @param $amt The amount of characters to be left un-obscured
* @param $amt_type The type of value that the amount is (as per $amt_types)
* @return The obscured string
*/
function obscure($string, $amt = 2, $amt_type = 'percent', $obscure_char = '*')
{
    if($amt_type = 'percent') { // If percentage, calculate an actual number
        if($amt > 100) // If the amount is greater than 100%, return all obscured
            $out = '';
            for($i = 0; $i < strlen($string); $i++) {
                $out .= $obscure_char;
            }
            return $out;
         if ($amt < 0) // If the amount is less than 100%, return the unchanged string
            return $string;
        $amt = $amt*0.01; // Turn from percentage into decimal
        $amt = round(strlen($string) * $amt); // Calculate number of characters to not obscure (replace round() with floor() to round down)
    } else {
        if($amt > strlen($string))
            $amt = strlen($string);
    }

    $chars = array(); // Will contain the string as separate characters
    $obscured = array(); // Will contain a list of which characters are already obscured

    for($i = 0; $i < strlen($string); $i++) {
        $chars[] = substr($string, $i, 1); // Split string into array
    }

    for($i = 0; $i < $amt; $i++) { // Obscure characters, but don't obscure the same one twice
        do
        {
            $rand = rand(0, strlen($string));
        } while (array_search($rand, $obscured));
$obscured[] = $rand;
        $chars[$rand] = $obscure_char;
    }
    return implode('', $chars); // Convert the array back into a string
}

echo obscure('This is a test string', 5, 'percent');

 

Link to comment
Share on other sites

Try this out. I just wrote it up without testing it, so it may have some minor errors, but the idea is what you're after.

<?php
/**
* Obscure characters in a string
*
* @param $string The string to be obscured
* @param $amt The amount of characters to be left un-obscured
* @param $amt_type The type of value that the amount is (as per $amt_types)
* @return The obscured string
*/
function obscure($string, $amt = 2, $amt_type = 'percent', $obscure_char = '*')
{
    if($amt_type = 'percent') { // If percentage, calculate an actual number
        if($amt > 100) // If the amount is greater than 100%, return all obscured
            $out = '';
            for($i = 0; $i < strlen($string); $i++) {
                $out .= $obscure_char;
            }
            return $out;
         if ($amt < 0) // If the amount is less than 100%, return the unchanged string
            return $string;
        $amt = $amt*0.01; // Turn from percentage into decimal
        $amt = round(strlen($string) * $amt); // Calculate number of characters to not obscure (replace round() with floor() to round down)
    } else {
        if($amt > strlen($string))
            $amt = strlen($string);
    }

    $chars = array(); // Will contain the string as separate characters
    $obscured = array(); // Will contain a list of which characters are already obscured

    for($i = 0; $i < strlen($string); $i++) {
        $chars[] = substr($string, $i, 1); // Split string into array
    }

    for($i = 0; $i < $amt; $i++) { // Obscure characters, but don't obscure the same one twice
        do
        {
            $rand = rand(0, strlen($string));
        } while (array_search($rand, $obscured));
$obscured[] = $rand;
        $chars[$rand] = $obscure_char;
    }
    return implode('', $chars); // Convert the array back into a string
}

echo obscure('This is a test string', 5, 'percent');

 

 

Sweet bro! Thanks. Looks like itll work. I havent testedd it yet, Im so tired just got off work Im going to test it in the morning Ill post here the results.

Link to comment
Share on other sites

Ok so Im about to test this. I have a few questions to better understand the code.

 

$amt_type, can you explain that a little more? What if the string is not a percentage?

 

What Im trying to do is get a value from a database. Each user has a random generated number from 100-300. I need one digit to be displayed. I love that you put a variable to set the characters that are obscured haha thats pretty cool.

 

But yeah thats what im trying to do. Substr itself just doesnt work, I tried everything. It looks like this code will work great. So for line 1, $string would look something like this?

<?php
$fetchdata= mysql_fetch_array($result)
$num= $fetchdata['fixednum'];
#
#
//
function obscure($num, $amt = 2, $amt_type = 'number', $obscure_char = '*')
?>

Im really interested in this code and am determined to make it work! Thanks for all your help I really appreciate it.

Link to comment
Share on other sites

What the amount was supposed to be for is the amount of un-obscured characters. If $amt_type is set to percentage, the function would find an actual whole number of characters that correspond to the percentage amount. Basically, if you said you wanted 50% of the characters un-obscured, and the string was twenty characters long, it would obscure 10 characters.

 

I see that it's obscuring all the characters anyways, and I'm not too sure why, but I'll take a look at it soon.

Link to comment
Share on other sites

What the amount was supposed to be for is the amount of un-obscured characters. If $amt_type is set to percentage, the function would find an actual whole number of characters that correspond to the percentage amount. Basically, if you said you wanted 50% of the characters un-obscured, and the string was twenty characters long, it would obscure 10 characters.

 

I see that it's obscuring all the characters anyways, and I'm not too sure why, but I'll take a look at it soon.

Oh okay I see. Im about to upload it and test it out,but you said its hiding all the digits ill try to tweak it and see if I can come up with anything.

 

Im pretty amazed that I found something that PHP just doesnt seem it can do. Ive been working on this for three days now searching and reading and coding and uploading and editing and reuploading and refreshing and overwriting god im getting so damn frustrated! This is something Im not going to give up on. This is an important part of my app that ive been writing for the past 8 months!!!

Link to comment
Share on other sites

Ok,well after playing around with it a little, I deleted everything that had to do with the percentage, just was confusing for me so I figured Id try that. Lol, well, its returning digits, but its still off. I changed the $amt to 1, the string i was using was 492. It was random, everytime I reloaded it would be different, with one character being displayed and three obscured chars, or with 2 chars being displayed with one or two obscured chars.

 

Very interesting... pretty stuck now though. I tried obscuring a mysql value, and nothing was even displayed. I think its impossible I cant believe it!!

Link to comment
Share on other sites

It's not impossible! It's just that we haven't figured out how to do it right yet. I'm sure that if some professional programmer looked at this, they would know exactly how to do it.

 

Im about to post it on freelancer.com. Thats all I can think to do! I could possibly do this thhrough mysqls substring, but I read the manual and its just too confusing and doesnt explain it all too well

Link to comment
Share on other sites

It's not impossible! It's just that we haven't figured out how to do it right yet. I'm sure that if some professional programmer looked at this, they would know exactly how to do it.

 

Im about to post it on freelancer.com. Thats all I can think to do! I could possibly do this thhrough mysqls substring, but I read the manual and its just too confusing and doesnt explain it all too well

 

Give me another try before you go spending money...

Link to comment
Share on other sites

Got it! :)

<?php
function obscure($string, $num_to_obscure = -1, $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
   }
   
   $string = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);
   $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);
}

 

And my test that proves it works:

<?php
$str = 'Random String to be obscured...';

echo 'String before obscuring: ' . $str;
echo '<br />';
echo 'String after obscuring: ' . obscure($str);

 

Here are three different outputs (reloads).

String before obscuring: Random String to be obscured...

String after obscuring: **n*om S*ri** *o****ob**u**d...*

 

String before obscuring: Random String to be obscured...

String after obscuring: Ran**m **r**g**o**e o**c*re**..*

 

String before obscuring: Random String to be obscured...

String after obscuring: ****om S***ng *o*b* obscu***.*.**

 

This is one function that I'm going to keep somewhere, because it may come in handy sometime... :)

Link to comment
Share on other sites

I've turned it into a class with two distinct functions. It doesn't really need to be in a class, but I wanted to upload it to phpclasses.org.

<?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($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($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

 

Link to comment
Share on other sites

Got it! :)

<?php
function obscure($string, $num_to_obscure = -1, $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
   }
   
   $string = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);
   $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);
}

 

And my test that proves it works:

<?php
$str = 'Random String to be obscured...';

echo 'String before obscuring: ' . $str;
echo '<br />';
echo 'String after obscuring: ' . obscure($str);

 

Here are three different outputs (reloads).

String before obscuring: Random String to be obscured...

String after obscuring: **n*om S*ri** *o****ob**u**d...*

 

String before obscuring: Random String to be obscured...

String after obscuring: Ran**m **r**g**o**e o**c*re**..*

 

String before obscuring: Random String to be obscured...

String after obscuring: ****om S***ng *o*b* obscu***.*.**

 

This is one function that I'm going to keep somewhere, because it may come in handy sometime... :)

Omg you got it! Thanks bro. I really appreciate it! Finally we got to the bottom of this now I can move on to the next step of my application!

 

The only thing with this, is its displaying more chars than the actual string. Im using a 3 digit number in a database, & its displaying sometimes 4 total chars. I set the $num_to_obscure to 2. For this instance I want to display one digit. When I refresh the page its random, which is great, but its displaying sometimes 2 digits, or sometimes one digit with two "*" characters.

But, Im defintly not bitching! This is great, thank you so much for your help and your code!!!!!!

Link to comment
Share on other sites

I've turned it into a class with two distinct functions. It doesn't really need to be in a class, but I wanted to upload it to phpclasses.org.

<?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($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($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

 

Good idea. Im saving this and putting it on my hard drive. This will come in handy in the future as well.

 

I noticed when you set the $obscure_char to blank it displays nothing but the string letters/digits, leaving the obscured characters out without being replaced by a char such as "*". Im not sure if this is self explanatory or not, but I think it would be something worth referencing in the class.

 

I need to ask, how did you learn PHP? How did you get to the level of expertise your on? I ask every good coder this that has helped me out. I love coding it is my 100% most favorite thing to do, & I really want to master it. I would like to make a career out of it. I already have made alot of money by releasing simple/cool little apps & creating/editing code for people,and cant imagine what else I could do with more knowledge. I think im stuck where im at, sometimes I look at peoples code and wonder how they can write so fluently.

Link to comment
Share on other sites

Good idea. Im saving this and putting it on my hard drive. This will come in handy in the future as well.

 

I noticed when you set the $obscure_char to blank it displays nothing but the string letters/digits, leaving the obscured characters out without being replaced by a char such as "*". Im not sure if this is self explanatory or not, but I think it would be something worth referencing in the class.

 

I need to ask, how did you learn PHP? How did you get to the level of expertise your on? I ask every good coder this that has helped me out. I love coding it is my 100% most favorite thing to do, & I really want to master it. I would like to make a career out of it. I already have made alot of money by releasing simple/cool little apps & creating/editing code for people,and cant imagine what else I could do with more knowledge. I think im stuck where im at, sometimes I look at peoples code and wonder how they can write so fluently.

 

I taught myself PHP using online tutorials such as W3Schools.com. I took programming classes in highschool for Turing and Java, which only helped me understand the concept of classes. I'm still learning, as any good programmer, about new stuff every time I write some code.

 

Regarding writing code fluently, it takes practice, but some people still cannot grasp the concept. It requires very logical thinking for the most part, and it shows in other parts of my life (I don't get emotional, I try to think of a logical response to everyday situations).

 

I'm going to take a look at the issue you're describing now.

Link to comment
Share on other sites

I believe that your issue can be solved by using trim() before sending it through the obscuring function.

 

Here's an updated class

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

Link to comment
Share on other sites

I believe that your issue can be solved by using trim() before sending it through the obscuring function.

 

Here's an updated class

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

 

Oh thats not an issue, its good actually if you want only the digits to be displayed with  no obscured chars (in case the end user cant know how long the string is).

 

But thats cool thats how I learned so far, thats what It seem every good coder says, they taught themselves online. I just need to start looking harder online for more tutorials.

 

Thanks for all your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.