if(preg_match($pattern, $subject, $matches) === true){
Should be
if(preg_match($pattern, $subject, $matches) === 1){
because the function returns the number of matches, not true or false.
For $pattern you give a regular expression that you want to match, so for any spaces
if(preg_match('/^\s+$/', $subject, $matches) === 1){
$subject is just the string you want to check in, so this would be your messages body or usename or what ever.
$matches is the thing I said you don't need
if(preg_match('/^\s+$/', $subject) === 1){
// $subject contains at least one space.
}