emilg1984 Posted February 7, 2014 Share Posted February 7, 2014 Page 444 example 3: I don't understand why the regular expression of checking if a string contains no spaces doesn't work for me. This works fine and prints TRUE: $pattern = '/^\S$/'; $subject = 'a'; But this prints FALSE: $pattern = '/^\S$/'; $subject = 'aa'; It doesn't have any spaces, so why is it FALSE? Link to comment Share on other sites More sharing options...
Antonio Conte Posted February 7, 2014 Share Posted February 7, 2014 Jon or Larry might give you some feedback on the regular expression. If you only want to solve the problem, you could consider using a function like strpos() instead. Something like this should work excellent: $subject = "AStringWithA spaceInIt"; $pattern = " "; if ( strpos($subject, $pattern) === true ) { echo "Pattern found in subject"; } else { echo "No pattern found in subject"; } The function is not binary safe, that's the reasoning behind checking for === true. Hope that helps. Link to comment Share on other sites More sharing options...
HartleySan Posted February 7, 2014 Share Posted February 7, 2014 By pure coincidence, I just answered a very similar question on the JavaScript board, but basically, /^\S$/ does not work for 'aa' because that regex only works for a single character. There are several variations on the regex you're using though that will work. For starters, you can do /^\S+$/, which will confirm that all characters are non-whitespace characters, regardless of the length of the string. Similarly, and more simply, you can use /\s/, which will search for any whitespace character in the string, meaning that if there is even one whitespace character in the string, then the regex will return the match. Like Antonio said though, the best solution to this problem is to use the strpos function, which does not require a regex, thus it will be faster, and it's much simpler to code. Just a small tweak to Antonio's code though: strpos returns a number on success and false on no match. As such, it's best to use the following to confirm a match: if (strpos($str, ' ') !== false) { // Space found. } As a final note, "space" and "whitespace character" have different meanings. I assumed you meant "space" for what you want to accomplish, even though the regex metacharacter \s means "whitespace character". 1 Link to comment Share on other sites More sharing options...
emilg1984 Posted February 7, 2014 Author Share Posted February 7, 2014 I don't understand why Larry uses this specific regex example to find spaces in a string if it can only work for a single character (and it is "space" in the example). Using stropos() makes much more sense. Thanks for the help Antonio and HartleySan, much appreciated. Link to comment Share on other sites More sharing options...
HartleySan Posted February 8, 2014 Share Posted February 8, 2014 I agree. Hopefully Larry can comment. Link to comment Share on other sites More sharing options...
Larry Posted February 10, 2014 Share Posted February 10, 2014 I don't have the text in front of me, but I expect I just mistyped and that I meant "isn't a space" not "doesn't contain a space". I'll double-check this and add it to the errata if it's a mistake. Link to comment Share on other sites More sharing options...
Recommended Posts