【问题标题】:Regex saying there are spaces in text when there is none正则表达式说没有空格时文本中有空格
【发布时间】:2013-03-01 16:51:49
【问题描述】:

我正在尝试检查字符串中的空格、制表符等,并且正在使用这个:

if (preg_match('/\s*/', $username)) {
    echo 'no spaces allowed in username.';
}

但是它总是认为用户名中有空格,即使没有。

我做错了什么?

【问题讨论】:

  • 你有没有'Doh!'时刻
  • @ChrisMoutray 啊,是的,我做到了,哈哈......一直在使用 * 太多,忘记了 + 是我在这种情况下需要的,哈哈

标签: php regex preg-match whitespace


【解决方案1】:

* 表示零次或多次。

所以

if (preg_match('/\s+/', $username)) {

【讨论】:

  • 打败我。虽然,实际上,要检查是否存在空间,您根本不需要量词。
猜你喜欢
  • 2020-04-06
  • 2017-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
相关资源
最近更新 更多