How would i use the php function preg_match() to test a string to see if any spaces exist?
我将如何使用php函数preg_match()来测试一个字符串,看看是否存在任何空格?
example
例子
"this sentence would be tested true for spaces"
"这句话将在空格中被检验"
"thisOneWouldTestFalse"
“thisOneWouldTestFalse”
6 个解决方案
#1
46
If you're interested in any white space (including tabs etc), use \s
如果你对任何空白(包括标签等)感兴趣,请使用\s
if (preg_match("/\\s/", $myString)) {
// there are spaces
}
if you're just interested in spaces then you don't even need a regex:
如果你只是对空间感兴趣,那么你甚至不需要一个regex:
if (strpos($myString, " ") !== false)
#2
4
Also see this * question that addresses this.
还可以看到这个*问题解决了这个问题。
And, depending on if you want to detect tabs and other types of white space, you may want to look at the perl regular expression syntax for things such as \b \w and [:SPACE:]
而且,根据您是否希望检测制表符和其他类型的空白,您可能需要查看perl正则表达式语法,例如\b \w和[:space:]
#3
0
[\\S]
upper case- 'S' will surely work.
大写字母“S”肯定有用。
#4
0
How about using ctype_graph for this purpose? This would expand the scope of space to mean any "white space char" that does not print anything visible on screen (like \t, \n ) also. However this is native and should be quicker than preg_match.
为此使用ctype_graph怎么样?这将扩展空间的范围,以表示任何“空白字符”,不打印屏幕上可见的任何内容(比如\t, \n)。但是这是本地的,应该比preg_match快。
$x = "string\twith\tspaces" ;
if(ctype_graph($x))
echo "\n string has no white spaces" ;
else
echo "\n string has spaces" ;
#5
0
You can use:
您可以使用:
preg_match('/[\s]+/',.....)
#6
0
faster to use:
更快的使用:
strstr($string, ' ');
strstr($ string”、“);
#1
46
If you're interested in any white space (including tabs etc), use \s
如果你对任何空白(包括标签等)感兴趣,请使用\s
if (preg_match("/\\s/", $myString)) {
// there are spaces
}
if you're just interested in spaces then you don't even need a regex:
如果你只是对空间感兴趣,那么你甚至不需要一个regex:
if (strpos($myString, " ") !== false)
#2
4
Also see this * question that addresses this.
还可以看到这个*问题解决了这个问题。
And, depending on if you want to detect tabs and other types of white space, you may want to look at the perl regular expression syntax for things such as \b \w and [:SPACE:]
而且,根据您是否希望检测制表符和其他类型的空白,您可能需要查看perl正则表达式语法,例如\b \w和[:space:]
#3
0
[\\S]
upper case- 'S' will surely work.
大写字母“S”肯定有用。
#4
0
How about using ctype_graph for this purpose? This would expand the scope of space to mean any "white space char" that does not print anything visible on screen (like \t, \n ) also. However this is native and should be quicker than preg_match.
为此使用ctype_graph怎么样?这将扩展空间的范围,以表示任何“空白字符”,不打印屏幕上可见的任何内容(比如\t, \n)。但是这是本地的,应该比preg_match快。
$x = "string\twith\tspaces" ;
if(ctype_graph($x))
echo "\n string has no white spaces" ;
else
echo "\n string has spaces" ;
#5
0
You can use:
您可以使用:
preg_match('/[\s]+/',.....)
#6
0
faster to use:
更快的使用:
strstr($string, ' ');
strstr($ string”、“);