EternalSorrow Posted November 15, 2009 Share Posted November 15, 2009 This should be an easy problem to solve for most, but I'm baffled by the idea of whitespaces in i$fields. What I want to know is how to find if a $field contains whitespace (just normal spaces, in this case) so I can perform a simple IF statement, like so: if ($row['title'] has [[whitespace]]) { $space = ' '; } else { $space = ''; } Thanks in advance for any help. Quote Link to comment https://forums.phpfreaks.com/topic/181654-solved-if-whitespace-in-field-do-this/ Share on other sites More sharing options...
michaellunsford Posted November 15, 2009 Share Posted November 15, 2009 <?php if(preg_match('/\s/',$row['title'])) { $space = ' '; } else { $space = ''; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/181654-solved-if-whitespace-in-field-do-this/#findComment-958139 Share on other sites More sharing options...
ohdang888 Posted November 15, 2009 Share Posted November 15, 2009 this should work to find it: $mystring = 'abc '; $findme = ' '; $pos = strpos($mystring, $findme); if($pos == true){ //there is white space } Quote Link to comment https://forums.phpfreaks.com/topic/181654-solved-if-whitespace-in-field-do-this/#findComment-958141 Share on other sites More sharing options...
smerny Posted November 15, 2009 Share Posted November 15, 2009 could simplify the last one to this if(strpos($row['title'], " ")) echo 'there is a space'; else echo 'there is not a space'; Quote Link to comment https://forums.phpfreaks.com/topic/181654-solved-if-whitespace-in-field-do-this/#findComment-958143 Share on other sites More sharing options...
michaellunsford Posted November 15, 2009 Share Posted November 15, 2009 strpos won't work if the space is at the front. It also won't identify a tab, newline, or return - aka: the other whitespace ;-) Quote Link to comment https://forums.phpfreaks.com/topic/181654-solved-if-whitespace-in-field-do-this/#findComment-958144 Share on other sites More sharing options...
EternalSorrow Posted November 15, 2009 Author Share Posted November 15, 2009 Thanks so much for the quick responses! Topic is officially solved! Quote Link to comment https://forums.phpfreaks.com/topic/181654-solved-if-whitespace-in-field-do-this/#findComment-958149 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.