I'm not too proficient with reg exp's, so it could be possible using them. But here's one way of doing things that would get rid of all the spaces and return the length (untested but should work):
function get_length($theString)
{
$theString = trim($theString);
$theString = str_replace(" ","",$theString);
return strlen($theString);
}
Hope this helps 😉