【发布时间】:2019-11-20 23:37:08
【问题描述】:
我正在尝试用除文本中最后 4 位数字之外的星号替换手机号码,并且该文本是动态的。
Eg. John's Mobile number is 8767484343 and he is from usa.
Eg. John's Mobile number is +918767484343 and he is from india.
Eg. Sunny's Mobile number is 08767484343 and he is from india.
Eg. Rahul's Mobile number is 1800-190-2312 and he is from india.
$dynamic_var = "John's Mobile number is 8767484343 and he is from usa.";
$number_extracted = preg_match_all('!\d+!', $dynamic_var , $contact_number);
// don't know what to do next
Result will be like Eg. John's Mobile number is ******4343 and he is from usa. Eg. John's Mobile number is ******4343 and he is from india. Eg. Sunny's Mobile number is ******4343 and he is from india. Eg. Rahul's Mobile number is ******2312 and he is from india.
【问题讨论】:
-
将号码视为手机号码的规则是什么?即我们可以有不同类型的号码手机号码、免费电话号码、固定电话号码等。
-
联系电话可以是任何国家代码或没有国家代码。和免费电话号码,如 1800-120-9878,如果找到任何带 + 或 - 或没有这些数字的数字,它应该替换除最后四个之外的整个数字。
-
它工作正常,但将最后四位数字替换为星号。我想将除最后四位之外的所有手机号码替换为星号。
-
好的,试试
preg_replace_callback('~\+?\d+(?:[-\s]*\d)*(?=(?:[-\s]*\d){4}(?!-?\d))~', function($m) { return preg_replace('~\d~', '*', $m[0]); }, $dynamic_var) -
但它没有从其他输入中替换 + 和 -