如何使用preg_match测试空格? [英] How do I use preg_match to test for spaces?

查看:181
本文介绍了如何使用preg_match测试空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何使用PHP函数preg_match()测试字符串以查看是否存在空格?

How would I use the PHP function preg_match() to test a string to see if any spaces exist?

此句子的空格将被测试为真"

"this sentence would be tested true for spaces"

" thisOneWouldTestFalse"

"thisOneWouldTestFalse"

推荐答案

如果您对任何空白(包括制表符等)感兴趣,请使用\s

If you're interested in any white space (including tabs etc), use \s

if (preg_match("/\\s/", $myString)) {
   // there are spaces
}

如果您只对空格感兴趣,那么您甚至不需要正则表达式:

if you're just interested in spaces then you don't even need a regex:

if (strpos($myString, " ") !== false)

这篇关于如何使用preg_match测试空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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