如何在文本中屏蔽信用卡号掩码? [英] How to mask credit card number mask in a text?

查看:43
本文介绍了如何在文本中屏蔽信用卡号掩码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一个表格,我的客户使用此表格向我发送消息.有时他们会在消息上写下他们的信用卡号.所以这真的很关键.我想屏蔽这些信用卡号.但当然卡号不会定期出现.

I have a form on my website and my customers send message to me with this form. Sometimes they write their credit card number on the message. So this is really critical. I want to mask these credit card numbers. But of course card numbers don't come on a regular basis.

示例 1:1111222233334444

Example 1: 1111222233334444

示例 2:4444 3333 2222 1111

Example 2: 4444 3333 2222 1111

示例 3:4444-3333-2222-1111

Example 3: 4444-3333-2222-1111

示例 4:4444 - 3333 - 2222 - 1111

Example 4: 4444 - 3333 - 2222 - 1111

示例 5:4444--3333--2222--1111

Example 5: 4444--3333--2222--1111

所以我可以屏蔽例如 1、2 和 3.但是如果数字之间有多个空格或破折号,我就不能.

So I can mask for example 1, 2 and 3. But if there are more than one space or dash between numbers I can't.

这是我的最后一个正则表达式:

And this is my last regex:

preg_replace("/(?:\b| )([3456]\d{3})([ -]+){0,1}\d{4}([ -]+){0,1}\d{4}([ -]+){0,1}(\d{0})/", "$1********$2", $a1);

以及这个正则表达式的结果:

And results for this regex:

结果 1:4444********1111

Result 1: 4444********1111

结果 2:4444******** 1111

Result 2: 4444******** 1111

结果 3:4444********-1111

Result 3: 4444********-1111

结果 4:4444******** - 1111

Result 4: 4444******** - 1111

结果 5:4444********--1111

Result 5: 4444********--1111

那么我应该在正则表达式中做什么?谢谢.

So what should I do in regex? Thanks.

推荐答案

我可以建议您将信用卡号码的验证与信用卡号码的显示分开,以便您的用户通过用户界面?假设您只存储了有效的信用卡号码,那么假设每个号码至少有 8 位数字可能是安全的.如果是这样,那么您可以使用一揽子正则表达式仅显示前 4 位和后 8 位数字:

May I suggest that you separate validation of your credit card number from the presentation of that number to your users via the UI? Assuming you have only stored valid credit card numbers, then it is probably safe to assume that every number has at least 8 digits. If so, then you can just use a blanket regex to only display the first 4 and last 8 digits:

$cc = "4444--3333--2222--1111";
echo preg_replace("/(\d{4}).*(\d{4})/", "$1********$2", $cc);

4444********1111

演示

您可能会指出,这会在每个卡号之间放置相同数量的星星.但是,话又说回来,这是一件好事,因为它使窥探者更难找出真正的未屏蔽号码.

You might point out that this puts the same number of stars in between every card number. But, then again, this is a good thing, because it makes it even harder for a snooper to fish out what the real unmasked number actually is.

这是一个更智能的正则表达式,它可以去掉任何数字的中间部分,只留下第一个和最后一个 4 个字符可见:

Here is a smarter regex which will star out the middle portion of any number, leaving only the first and last 4 characters visible:

$cc = "4444--3333--2222--1111";
echo preg_replace("/(?<=.{4}).(?=.{4})/", "*", $cc);

4444**************1111

请注意,此解决方案不会从 11114444 中删除任何内容作为理论输入.

Note that this solution would not remove anything from 11114444 as a theoretical input.

这篇关于如何在文本中屏蔽信用卡号掩码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆