如何用星号替换某些字符 [英] How To Replace Some Characters With Asterisks
本文介绍了如何用星号替换某些字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个与PHP有关的简单任务,但是由于我不熟悉正则表达式之类的东西,所以我不知道该怎么办.
I have a simple task to do with PHP, but since I'm not familiar with Regular Expression or something... I have no clue what I'm going to do.
我想要的实际上很简单...
what I want is very simple actually...
假设我有这些变量:
$Email = 'john@example.com'; // output : ****@example.com
$Email2 = 'janedoe@example.com'; // output : *******@example.com
$Email3 = 'johndoe2012@example.com'; // output : ***********@example.com
$Phone = '0821212121'; // output : 082121**** << REPLACE LAST FOUR DIGIT WITH *
如何使用PHP做到这一点?谢谢.
how to do this with PHP? thanks.
推荐答案
您将需要为每个函数提供特定的功能.对于邮件:
You'll need a specific function for each. For mails:
function hide_mail($email) {
$mail_segments = explode("@", $email);
$mail_segments[0] = str_repeat("*", strlen($mail_segments[0]));
return implode("@", $mail_segments);
}
echo hide_mail("example@gmail.com");
电话号码
function hide_phone($phone) {
return substr($phone, 0, -4) . "****";
}
echo hide_phone("1234567890");
看到了吗?没有使用单个正则表达式.这些功能虽然不检查有效性.您需要确定什么类型的字符串是什么,然后调用适当的函数.
And see? Not a single regular expression used. These functions don't check for validity though. You'll need to determine what kind of string is what, and call the appropriate function.
这篇关于如何用星号替换某些字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文