preg_match仅允许字母,空格,破折号和空格 [英] preg_match only letters, spaces and dashes and spaces allowed

查看:105
本文介绍了preg_match仅允许字母,空格,破折号和空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,如果$ stringabc包含数字以外的内容,即时消息当前会在其中使用的一个条件是:

i know that one condition that im currently using where it gives an error if $stringabc contains anything but numbers is :

if(preg_match("/[^0-9]/",$stringabc))

我想要一个if条件,如果$ stringdef包含除字母,空格和破折号(-)之外的任何内容,它将给出错误.

I want an if condition where it gives an error if $stringdef contains anything but letters, spaces and dashes (-).

推荐答案

那是:

if(preg_match('/[^a-z\s-]/i',$stringabc))

用于除字母(a-z),空格(\ s,表示任何种类的空白)和破折号(-)之外的任何字符".

for "anything but letters (a-z), spaces (\s, meaning any kind of whitespace), and dashes (-)".

还允许数字:

if(preg_match('/[^0-9a-z\s-]/i',$stringabc))

这篇关于preg_match仅允许字母,空格,破折号和空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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