关于php:如何删除数字字符之间的空格字符? | 珊瑚贝

how to remove space character between number character?

本问题已经有最佳答案,请猛点这里访问。


我有这样的字符串:

1
$str =“old iccid : 809831 3245 345 new iccid : 999000 112221”

PHP中如何去掉数字字符之间的空格,变成这个输出?

1
$output =“old iccid : 8098313245345 new iccid : 999000112221”;
  • 如果您的单词不混合字母和数字, preg_replace() 会这样做。


$output = preg_replace( ‘/\\d[ *]\\d/’, ”, $str);

  • 我又检查了一遍,那些代码没有正常工作..它在空格附近删除字符,结果如下: $output = “old iccid : 80983245345 new iccid : 9990012221”
  • 这世上什么!?!为什么这有3个赞成票?这无疑是错误的。证明:3v4l.org/DfJda 它对空格前的数字、空格和空格后的数字进行核对。


将数字与字符分开并使用

1
2
3
$str=“809831 3245 345”;
$foo= str_replace(‘ ‘,,$str);
echo $foo; // gives 8098313245345

  • $str =”old iccid : 809831 3245 345 new iccid : 999000 112221″ 的输出是什么???
  • @Havelock 这就是为什么我提到\\’从字符中分离数字\\’
  • 这是 100% 不正确的。证明:3v4l.org/ANLtJ 它删除了字符串中的所有空格,这不是预期的效果。


我从 xdazz (http://stackoverflow.com/users/762073/xdazz) 得到了答案,这个语法很好用。

1
$output = preg_replace(‘/(?<=\\d)\\s+(?=\\d)/’, , $str);
  • 复制其他人的答案并将其重复发布在 Stackoverflow 上——对于 Stackoverflow 或其研究人员来说没有价值。


来源:https://www.codenong.com/11928579/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8643.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?