Jump to content
Larry Ullman's Book Forums

Recommended Posts

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 post
Share on other sites

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";
   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 post
Share on other sites

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".

  • Upvote 1
Link to post
Share on other sites

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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...