fugix Posted April 25, 2011 Share Posted April 25, 2011 I'm looking for a function that will check a string for letters and spaces...boolean preferibly Link to comment https://forums.phpfreaks.com/topic/234691-check-for-letters-and-spaces/ Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2011 Share Posted April 25, 2011 Check that a string contains letters and spaces, or contains ONLY letters and spaces? Link to comment https://forums.phpfreaks.com/topic/234691-check-for-letters-and-spaces/#findComment-1206044 Share on other sites More sharing options...
fugix Posted April 25, 2011 Author Share Posted April 25, 2011 sorry for not clarifying...contains ONLY letters and spaces...i tried ctype_alpha() but this returns false due to the space. Link to comment https://forums.phpfreaks.com/topic/234691-check-for-letters-and-spaces/#findComment-1206053 Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2011 Share Posted April 25, 2011 This returns 1 if anything other than letters or spaces are in $your_string. Or at least it should (regex isn't one of my specialities by a long shot). preg_match( '~[^A-Za-z ]~', $your_string); Link to comment https://forums.phpfreaks.com/topic/234691-check-for-letters-and-spaces/#findComment-1206077 Share on other sites More sharing options...
Fadion Posted April 25, 2011 Share Posted April 25, 2011 I'm not a regular expressions expert, but the following code should work: <?php $s = 'php @freaks'; if (!preg_match('/[^a-z\s]/i', $s)) { echo 'Contains only Letters and Spaces!'; } else { echo 'Contains garbage data!'; } ?> The regex will return TRUE if it finds any characters except letters and spaces. If it finds only letters and spaces, will return FALSE. Link to comment https://forums.phpfreaks.com/topic/234691-check-for-letters-and-spaces/#findComment-1206078 Share on other sites More sharing options...
fugix Posted April 25, 2011 Author Share Posted April 25, 2011 thanks guys Link to comment https://forums.phpfreaks.com/topic/234691-check-for-letters-and-spaces/#findComment-1206089 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.