How to replace some character in mysql column by 'x'? - php

I have emails like this
uuak6G6GgD#gmail.com
d3lferM#gmail.com
Efqc9#gmail.com
How to convert and select like this.
uuakxxxxxx#xxxxx.xxx
d3lfxxx#xxxxx.xxx
Efqcx#xxxxx.xxx
Yes I am able to get this value but, I am not good enough in mysql function so please help me to make it simple and short if possible. And also suggest what other solution would be to make it.
I have created my query as
SELECT CONCAT(LEFT(email,
4),SUBSTR(REGEXP_REPLACE(email,'[a-z0-9]',"x"),5)),email
FROM `users`
I am using PHP as server side, so If we could involve php to make good enough, please also suggest.
Your help and suggestions are heartily appreciable.

If the point is obfuscation of email addresses, then you should not replace the characters by x, thus giving indication on the length of the email address. Just take the first 4 characters (left() function) and add a fixed ending of "xxxxxxxx#xxxx.xxx". You also need to decide how to handle email addresses where the user part is shorter than 4 characters.

If you want to achieve this using PHP you can simply use the following regex along with preg_replace function like as
(^(\w){1,4}|#|\.)(*SKIP)(*F)|(.)
Example :
echo preg_replace("/(^(\w){1,4}|#|\.)(*SKIP)(*F)|(.)/","$1x","uuak6G6GgD#gmail.com");

I would suggest something like :
$email = substr($email, 0, 4) . preg_replace('/\w/', 'x', substr($email, 4));
Doing it in the MySQL query is usually not reasonnable imo. MySQL isn't very good at that kind of data transformation.

$emails = [
'uuak6G6GgD#gmail.com',
'd3lferMGo7#gmail.com',
'Efqc90dUGI#gmail.com',
'I#gmail.com',
];
$modded = [];
foreach ($emails as $item) {
$name = explode('#', $item);
$name = str_pad(substr($name[0], 0, 4), 4, "x");
$modded[] = $name . 'xxxx#xxx.xxx';
}
var_dump($modded);

involve php, select record and than
Break every email address in two parts
$email = 'I#gmail.com';
$email = explode('#', $email);
$firstPart = $email[0];
$lastPart = $email[1];
$first4 = substr($firstPart, 0, 4);
$mid = preg_replace('/[^.#\-]/', 'x', substr($firstPart, 4));
$last = preg_replace('/[^.#\-]/', 'x', $lastPart);
$converted = $first4.$mid.'#'.$last;

Related

Why does the sub string not working? Is there way to sub string the result?

I have current module where I need to list all zip files from S3 AWS to the html tables. now I want to sub-string the 2 digit and the last digit of the number. however when i try to var dump the result still same number the substr not working can you help me guys to find out how to solved this?.
Example.
Number: 1150
Result must be: 11 and 50
I will show you guys my sample code that I already created.
$storage = Storage::disk('s3');
$client = $storage->getAdapter()->getClient();
$command = $client->getCommand('ListObjects');
$command['Bucket'] = $storage->getAdapter()->getBucket();
$command['Prefix'] = '11-10-2019';
$result = $client->execute($command);
foreach ($result['Contents'] as $file) {
$base_name = basename($file['Key']);
$trim_1 = str_replace('exp', '', $base_name);
$trim_2 = substr($trim_1, 1, -4);
var_dump($trim_2);
}
My Output is this:
$number = 1150;
$trim1 = substr($number, 0, 2);
$trim2 = substr($number,-2);
echo $trim1;
echo $trim2;
Based on what you ask you can achieve this by doing something like that.
The output is 11 & 50
Since you are using laravel you can use laravel helper for that which is pretty much the same and go for something like:
Str::substr($number, 2);
Str::substr($number, -2);
Don't forget to include the helper at the top of your file:
use Illuminate\Support\Str;

Cleaner way to use array_search?

I have the following string after it has been parsed by regex:
FOO_BAR_FOOBA_*_R:FOO_*
I am supposed to get rid of "FOOBA" so I thought that just by doing, should have been enough.
$size[0] = array('qwe', 'rty', 'uiop', 'asdf');
$size[1] = array('ASD' , 'FGHJ' , 'ZXCVB');
$size[2] = array('lol' , 'cat' , 'woof');
$pieces = explode("_",$noGen);
foreach ($size[0] as $value){
$search = array_search($size[0], $pieces);
unset($pieces[$search]);
}
However, I know that the code above pretty much would have worked out until I found out that not all the time the third "piece" exists and that the second piece sometimes can contain same values as the third one.
Is there a way to do this without having to use a foreach per size. If it is not clear I could add more information or just ping me.
Thank you in advance!

how to partially mask/hide email address using PHP

Im trying to achieve the following with PHP
sample#gmail.com => s*****#gmail.com
sa#yahoo.com => **#yahoo.com
sampleaddress#hotmail.com => samplead*****#hotmail.com
I want to hide last five characters in the portion that stays before '#'
I can write long code to do this by splitting and then replacing based on lengths, but Im sure there must be an easy way to do this using PHP functions, any help please?
UPDATE:
Im adding my code here, Im sure its not efficient, and thats the reason Im asking it here
$email = 'sampleuser#gmail.com';
$star_string = '';
$expl_set = explode('#',$email);
if(strlen ($expl_set[0]) > 5){$no_stars = 5; }else{$no_stars = strlen ($expl_set[0]); }
for($i=0;$i<$no_stars; $i++)
{
$star_string.='*';
}
$masked_email = substr($expl_set[0], 0, -5).$star_string.'#'.$expl_set[1];
You can wrap it into a function, making it easier to call multiple times.
Basically, split the address and the domain, replace $mask number of characters in the end of the string (default 5) with *, or the length of the address if it's shorter than the amount of masked characters.
function mask_email($email, $masks = 5) {
$array = explode("#", $email);
$string_length = strlen($array[0]);
if ($string_length < $masks)
$masks = $string_length;
$result = substr($array[0], 0, -$masks) . str_repeat('*', $masks);
return $result."#".$array[1];
}
The above would be used like this
echo mask_email("test#test.com")."\n";
echo mask_email("longeremail#test.com");
which would ouput this
****#test.com
longer*****#test.com
You can also specify the number you want filtered by using the second parameter, which is optional.
echo mask_email("longeremail#test.com", 2); // Output: longerema**#test.com
Live demo

PHP - How can I display time correctly?

I am using PHP to query an AS400 DB2 database. The time(ADACTM) is saved in the table like;
I need to convert this to a human-readable format like 9:46:23.
I am currently doing this in PHP;
$adactm = str_split($fin2['ADACTM']);
$adactm = "$adactm[0]$adactm[1]:$adactm[2]$adactm[3]:$adactm[4]$adactm[5]";
The problem is, when the time doesn't have a 2-digit hour, PHP thinks array position 0 is actually position 1. So the time shows like;
94:62:3
If anyone has a way to fix this, it would be greatly appreciated. Thanks!
Pad the string before you split it:
$rawtime = '94623';
$padded = str_pad($rawtime, 6, '0', STR_PAD_LEFT); // 094623
Then split/mangle as before
Sorry, no better idea than this:
$adactm = str_split($fin2['ADACTM']);
if (count($adactm) == 5) {
$adactm = "$adactm[0]:$adactm[1]$adactm[2]:$adactm[3]$adactm[4]";
} else {
$adactm = "$adactm[0]$adactm[1]:$adactm[2]$adactm[3]:$adactm[4]$adactm[5]";
}

Phone number format received from twilio

I am storing sms received from twilio in a database so I can use them later. When I did this in the sandbox it worked. However when I upgraded to a regular phone number the number received is the same as was sent to, but +1 (or for xxxxxxxxxx where the x's are the original number, it looks more like 1xxxxxxxxxx+)
I therefore changed the mysql_query to the following: but it is still not working. What can be done to recognize that this is the original phone number?
<?php
$starttime = time();
$number = $_POST['number'];
$number1 = "1" . $number;
$number2 = $number . "1";
$number3 = "+1" . $number;
$number4 = $number . "+1";
$number5 = "+" . $number . "1";
$number6 = "1" . $number . "+";
$number7 = $number."1+";
$received = mysql_query("SELECT * FROM sms_received
WHERE (responder='$number' OR responder='$number1'
OR responder='$number2' OR responder='$number3'
OR responder='$number4' OR responder='$number5'
OR responder='$number6' OR responder='$number6')
AND (body='y' OR body='yes' OR body='Y' OR body='Yes' OR 'yea' OR 'Yea')
AND timestamp BETWEEN ".date('Y-m-d H:i:s', strtotime($starttime))." AND NOW()");
?>
But still, nothing is being received. Any ideas how else I can check whether an sms has been received from the user? I can see in the database that it's there... but the mysql isn't finding it. It worked before, when the number sent was identical to the number received from, but with the added +1 it screws it up. (the code before just had WHERE responder = '$number' and it worked, but the additional variables didn't help it).
Does this code have too many OR's? Is that even a problem?
UPDATE:
Thanks, here is the function I'm using to strip the number down to xxxxxxxxxx format, before saving it to the database:
function checkPhone($responder){
$items = Array('/\ /', '/\+/', '/\-/', '/\./', '/\,/', '/\(/', '/\)/', '/[a-zA-Z]/');
$clean = preg_replace($items, '', $responder);
if (substr($clean, 0, 1) == "1") {
return substr($clean, 1, 10);
}
else {
return substr($clean, 0, 10);
}
}
$number = checkPhone($responder);
Twilio returns numbers in a format called E.164, which is an internationally recognized standard for phone number formatting.
In general, it's best practice to standardize the number to E164 BEFORE you store it in the database. That way you don't have to worry about storing different data with two different copies of the same number - eg 925-555-1234 and (925) 5551234.
Google has a libphonenumber library that will convert numbers for you. It works with Javascript, C++, Java, and Python.
If you are using PHP, and only using US/Canadian numbers, you can write a function to normalize phone numbers, that does something like the following:
- Strip out all non number characters from the phone number
(parentheses, dashes, spaces) - you can use a function like preg_replace
- if the phone number begins with a +1, do nothing
- if the phone number begins with a 1, add a +
- else, add a +1 to the beginning of the number.
- finally, store it in the database.
I hope that helps - please let me know if you have more questions.
Kevin
Your last or is redundantly $number6, it should be $number7.
Aside from that, you can do a few different things, such as in:
responder in ('$number', '$number1', '$number2', '$number3', '$number4', '$number5', '$number6', '$number7')
Or something like this:
responder like '%$number%'
Use a regular expression.
preg_match_all("{[0-9]+}",$number,$m);
$norm_num="+".implode($m[0]);
if(strlen($norm_num)<6)
exit('Too short!');
mysql_query("SELECT * FROM sms_received
WHERE responder='%$norm_num%'
AND body IN ('y','yes','Y','Yes','yea','Yea')
AND timestamp BETWEEN ".date('Y-m-d H:i:s', strtotime($starttime))." AND NOW()");

Categories