How do I use preg_match to test for spaces?

前端 未结 7 1389
Happy的楠姐
Happy的楠姐 2020-12-28 13:47

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

Example

"this sentence would be tested true for

7条回答
  •  青春惊慌失措
    2020-12-28 14:21

    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.

    $x = "string\twith\tspaces" ;
    if(ctype_graph($x))
        echo "\n string has no white spaces" ;
    else
        echo "\n string has spaces" ;
    

提交回复
热议问题