I am trying to create a way of making sure that every space has at least three characters (a-zA-Z and single quotes are allowed) on each side of it. It does exactly that, however only with the first space. Not the rest of them. I tried preg_match_all() to no avail, hence my question/post to you guys.
<?PHP
function validateSpaces($str) {
if ( strpos( $str, ' ' ) !== FALSE && !preg_match( '/(([a-z\']{3,})([ ]{1})([a-z\']{3,}))/i', $str ) )
return FALSE;
return TRUE;
}
echo validateSpaces( 'Hey There' ); // Valid (correct)
echo validateSpaces( 'He There' ); // Invalid (correct)
echo validateSpaces( 'Hey Ther e' ); // Valid (incorrect)
?>
As you can see, the first two examples are working like they should, but the second one validates although the second space only has one character on the right side of it. Which is not what I want.
Any help or attempt to help is greatly appreciated!
Thank you in advance,
Chris.
Last modification, will macth only if we have ony one space (trim string before trying to match it):
^([a-z']{3,} ?)+$