【问题标题】:Remove telephone numbers in a string masked with many spaces in PHP [closed]在PHP中删除用许多空格屏蔽的字符串中的电话号码[关闭]
【发布时间】: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


【解决方案1】:

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

使用的PHP函数:preg_replacestr_padstrlensubstr

$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

【讨论】:

  • 感谢您的回答,但它对我不起作用,因为在输出中我需要所有其他字符
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-10
  • 2013-07-13
  • 2018-05-07
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 2013-05-22
相关资源
最近更新 更多