【发布时间】:2017-06-23 18:16:22
【问题描述】:
我不会删除或屏蔽字符串中的电话号码,如果号码不带任何空格,那会很容易,但有时我会遇到这样的情况:
“M 的名字是 mike,我的手机是 1 2 3 4 5 6 7 8 9”
输出应该是这样的:
“我叫 mike,手机是 *******89”
【问题讨论】:
-
所以先用一种方法去掉空格;有很多
标签: php string number-formatting phone-number
我不会删除或屏蔽字符串中的电话号码,如果号码不带任何空格,那会很容易,但有时我会遇到这样的情况:
“M 的名字是 mike,我的手机是 1 2 3 4 5 6 7 8 9”
输出应该是这样的:
“我叫 mike,手机是 *******89”
【问题讨论】:
标签: php string number-formatting phone-number
您可以使用正则表达式替换空格
使用的PHP函数:preg_replace、str_pad、strlen、substr
$string = '1 2 3 4 5 6 7 8 9';
$string = preg_replace("/\s*/", "", $string ); //use preg_replace to replace spaces
$string = str_pad('', strlen($string)-2, '*') . substr($string, -2); // replace all without the 2 last chars with '*' and concatenate last 2 chars
【讨论】: