【问题标题】:How to replace a whitespace in chosen username? [duplicate]如何替换所选用户名中的空格? [复制]
【发布时间】:2017-02-18 11:53:02
【问题描述】:

我想用没有空格的空格替换选择的用户名。

preg_replace("[ ]", "", $username);

【问题讨论】:

  • 你可以试试preg_replace('/\s+/', '', $username);
  • 你能解释一下'/\s+/'是什么意思吗
  • 它正在搜索空格\s+ says "match a sequence, made up of one or more space characters"
  • 这个链接能解决你的问题吗?

标签: php preg-replace


【解决方案1】:

您可以按照下面提供的任何方法从字符串中删除空格。

  1. 对于空格,使用str_replace: str_replace()

$chosen_string = str_replace(' ', '', $chosen_string);

  1. 对于所有空格,使用preg_replace: preg_replace()

$chosen_string = preg_replace('/\s+/', '', $chosen_string);

备忘单参考:

【讨论】:

  • 它是正确的那为什么 -ve 标记? +1
  • 请说明为什么 -ve 标记到这个答案
  • 对于我的回答中的-ve 标记仍然没有任何解释:(
  • 也许是因为它是重复的,答案可以作为评论。
猜你喜欢
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-03
  • 1970-01-01
  • 2018-10-19
相关资源
最近更新 更多