sorenchr Posted August 8, 2008 Share Posted August 8, 2008 Hi there, im having a bit of trouble with strings consisting of nothing else but blank spaces. Since im using a rather large function, i've decided not to bother you with the more complex details and just make a little example that illustrates my problem. Im looking for a function that will target and delete strings containing nothing more but spaces (1 or more), i've tried the following: $string = " "; str_replace(' ', '', $string); if(empty($string)) { echo "empty!"; } else { echo "spacy, i say"; } Im absolutely out of ideas of what to do, since this obviously doesn't work. Anybody got a solution? Link to comment https://forums.phpfreaks.com/topic/118844-killing-spaces-in-strings/ Share on other sites More sharing options...
papaface Posted August 8, 2008 Share Posted August 8, 2008 <?php $string = " "; $string = trim(str_replace(' ', '', $string)); if(empty($string)) { echo "empty!"; } else { echo "spacy, i say"; } ?> Link to comment https://forums.phpfreaks.com/topic/118844-killing-spaces-in-strings/#findComment-612011 Share on other sites More sharing options...
sorenchr Posted August 8, 2008 Author Share Posted August 8, 2008 Sir, i say sir, you are a genius Link to comment https://forums.phpfreaks.com/topic/118844-killing-spaces-in-strings/#findComment-612012 Share on other sites More sharing options...
DarkWater Posted August 8, 2008 Share Posted August 8, 2008 I don't think you even need the str_replace. Try just trim(). Link to comment https://forums.phpfreaks.com/topic/118844-killing-spaces-in-strings/#findComment-612022 Share on other sites More sharing options...
papaface Posted August 8, 2008 Share Posted August 8, 2008 Sir, i say sir, you are a genius Thanks, as a side note. str_replace(' ', '', $string) is not required when using trim(). So use: <?php $string = " "; $string = trim($string); if(empty($string)) { echo "empty!"; } else { echo "spacy, i say"; } ?> Link to comment https://forums.phpfreaks.com/topic/118844-killing-spaces-in-strings/#findComment-612023 Share on other sites More sharing options...
rrmosby Posted August 8, 2008 Share Posted August 8, 2008 I thought trim only got rid of spaces at the beginning or end, not in the middle? Link to comment https://forums.phpfreaks.com/topic/118844-killing-spaces-in-strings/#findComment-612041 Share on other sites More sharing options...
DarkWater Posted August 8, 2008 Share Posted August 8, 2008 If the string is all spaces, it doesn't matter. Link to comment https://forums.phpfreaks.com/topic/118844-killing-spaces-in-strings/#findComment-612043 Share on other sites More sharing options...
papaface Posted August 8, 2008 Share Posted August 8, 2008 I thought trim only got rid of spaces at the beginning or end, not in the middle? If you need to remove absolutely all spaces keep your str_replace(). Link to comment https://forums.phpfreaks.com/topic/118844-killing-spaces-in-strings/#findComment-612047 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.