需要PHP preg_match,以确保单词之间仅允许一个空格字符 [英] PHP preg_match needed to ensure only ONE space character is allowed between words

查看:151
本文介绍了需要PHP preg_match,以确保单词之间仅允许一个空格字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我需要一个preg_match,以确保每个单词之间只能有一个空格字符(如果有多个单词).

basically I need a preg_match that ensures that only one space character is allowed between each word (IF there is more than one word).

我现有的规则是:

(^[A-Za-z\s]+$)

谢谢.

推荐答案

这取决于您单词"的含义,但是假设您使用A-Z或a-z字母表示,则可以尝试以下操作:

It depends what you mean by "word", but assuming you mean letters in A-Z or a-z you can try this:

^[a-zA-Z]+( [a-zA-Z]+)*$

请注意,\s并非 表示空格字符-表示任何空格,包括换行符.

Note that \s does not mean the space character - it means any whitespace, including a new line.

这篇关于需要PHP preg_match,以确保单词之间仅允许一个空格字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆