Tuesday, July 12, 2016

Function to Mask Numbers in PHP

Here is function to mask numbers

//////Code

arguments
$phonenumber : number to mask
$trim = number of digit to show
$maskCharacter = character to be displayed on masked digits

function maskNumber($phonenumber, $trim, $maskCharacter)
{
$str="";
$suffixNumber = substr($phonenumber, strlen($phonenumber)-$trim,$trim);
$prefixNumber = substr($phonenumber, 0, -$trim);

for ($x = 0; $x < strlen($prefixNumber); $x++):
    $str.= ( is_numeric($prefixNumber[$x]) )? str_replace($prefixNumber[$x], $maskCharacter, $prefixNumber[$x]) : $prefixNumber[$x];
endfor;

return  $str.$suffixNumber;    
}


HOW TO USE

Suppose you have large database of Mobile number of any other database which has to partially shown on the site or documents.then you can mask the number or string to protect your data

Example

IF I have an array for which i need to show only last four digits

$telarray = array('7383204940','7383323653','7878389652','8000306647','8000658282');

I would call it as

for($i=0;$i<count($telarray);$i++){
echo $masked = maskNumber($telarray[$i],4,'X');
echo '<br />';
}

OUTPUT would be

XXXXXX4940
XXXXXX3653
XXXXXX9652
XXXXXX6647
XXXXXX8282

Thanks

Cheers

upload a zip file and unzipping it in PHP

How to upload a zip file and unzip it at server in php

Here is function to upload and unzip

HTML body

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Unzip a zip file to Server</title>
    </head>
   
    <body>
    <?php if($message) echo "<p>$message</p>"; ?>
    <form enctype="multipart/form-data" method="post" action="">
    <label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
    <br />
    <input type="submit" name="submit" value="Upload" />
    </form>
    </body>

    </html>

PHP CODE

//recursive function

 function rmdir_recursive($dir) {
    foreach(scandir($dir) as $file) {
    if ('.' === $file || '..' === $file) continue;
    if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
    else unlink("$dir/$file");
    }
   
    rmdir($dir);
    }
   
// upload and check the file is in valid format 
 
  if($_FILES["zip_file"]["name"]) {
    $filename = $_FILES["zip_file"]["name"];
    $source = $_FILES["zip_file"]["tmp_name"];
    $type = $_FILES["zip_file"]["type"];
   
    $name = explode(".", $filename);
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
    foreach($accepted_types as $mime_type) {
    if($mime_type == $type) {
    $okay = true;
    break;
    }
    }
   
    $continue = strtolower($name[1]) == 'zip' ? true : false;
    if(!$continue) {
    $message = "The file you are trying to upload is not a .zip file. Please try again.";
    }
   
    /* PHP current path */
    $path = dirname(__FILE__).'/'; // absolute path to the directory where zipper.php is in
    $filenoext = basename ($filename, '.zip'); // absolute path to the directory where zipper.php is in (lowercase)
    $filenoext = basename ($filenoext, '.ZIP'); // absolute path to the directory where zipper.php is in (when uppercase)
   
    $targetdir = $path . $filenoext; // target directory
    $targetzip = $path . $filename; // target zip file
   
    /* create directory if not exists', otherwise overwrite */
    /* target directory is same as filename without extension */
   
    if (is_dir($targetdir)) rmdir_recursive ( $targetdir);
   
   
    mkdir($targetdir, 0777);



//UNZIPPING THE ZIP FILE CODE

if(move_uploaded_file($source, $targetzip)) {
    $zip = new ZipArchive();
    $x = $zip->open($targetzip); // open the zip file to extract
    if ($x === true) {
    $zip->extractTo($targetdir); // place in the directory with same name
    $zip->close();
   
    unlink($targetzip);
    }
    $message = "Your .zip file was uploaded and unpacked.";
    } else {
    $message = "There was a problem with the upload. Please try again.";
    }
    }


Finished!!
cheers