在PHP中删除带有许多空格的字符串中的电话号码

在PHP中删除带有许多空格的字符串中的电话号码,php,string,number-formatting,phone-number,Php,String,Number Formatting,Phone Number,我习惯于删除或屏蔽字符串中的电话号码,如果号码没有空格,那么写起来会很容易,但有时我会有这样的情况: 我叫迈克,我的电话是123456789 输出应如下所示: 我的名字是迈克,我的电话是*******89 可以使用正则表达式替换空格 使用的PHP函数:preg_replace、str_pad、strlen、substr 所以先用一种方法去掉空格;答案有很多答案,但对我来说不起作用,因为在输出中我需要所有其他字符 $string = '1 2 3 4 5 6 7 8 9'; $string = p

我习惯于删除或屏蔽字符串中的电话号码,如果号码没有空格,那么写起来会很容易,但有时我会有这样的情况:

我叫迈克,我的电话是123456789

输出应如下所示:

我的名字是迈克,我的电话是*******89


可以使用正则表达式替换空格

使用的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