我正试图取代手机号码与星级除了最后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.
3条答案
按热度按时间62o28rlo1#
从我看到的示例输入和期望的输出来看,不需要
preg_replace_callback()
的开销,只要星号后面跟有4个或更多的数字或连字符,可变长度的前瞻就允许您一次用星号替换一个字符。代码:(Demo)
输出:
我可以想象一些我的代码片段无法处理的边缘情况,但无论何时,当您处理不遵守严格格式的电话号码时,您都将陷入一个充满挑战的兔子洞。
pengsaosao2#
你可以直接从你的$dynamic_var实现,例如:
c8ib6hqw3#
古老但有用...