curt22 Posted August 30, 2007 Share Posted August 30, 2007 How can I remove ALL whitespace (NOT just spaces) from a string, and Not just at the beginning and end. Quote Link to comment https://forums.phpfreaks.com/topic/67365-solved-remove-whitespace/ Share on other sites More sharing options...
pocobueno1388 Posted August 30, 2007 Share Posted August 30, 2007 <?php $str = "There should be no spaces"; $str = str_replace(' ', '', $str); echo $str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/67365-solved-remove-whitespace/#findComment-337973 Share on other sites More sharing options...
curt22 Posted August 30, 2007 Author Share Posted August 30, 2007 Thanks pocobueno1388, but I want to remove all kinds of whitespace like \t \n and all other space characters. Quote Link to comment https://forums.phpfreaks.com/topic/67365-solved-remove-whitespace/#findComment-337975 Share on other sites More sharing options...
effigy Posted August 30, 2007 Share Posted August 30, 2007 preg_replace('/\s+/', '', $content); will cover it as long as Unicode is not involved. Quote Link to comment https://forums.phpfreaks.com/topic/67365-solved-remove-whitespace/#findComment-337976 Share on other sites More sharing options...
Jessica Posted August 30, 2007 Share Posted August 30, 2007 I think you'll have to remove each one using str_replace. Quote Link to comment https://forums.phpfreaks.com/topic/67365-solved-remove-whitespace/#findComment-337977 Share on other sites More sharing options...
pocobueno1388 Posted August 30, 2007 Share Posted August 30, 2007 This should give you what you want. <?php $str = "There should be no spaces"; $str = str_replace(array("\n", "\r", "\t", " "), '', $str); echo $str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/67365-solved-remove-whitespace/#findComment-337985 Share on other sites More sharing options...
curt22 Posted August 30, 2007 Author Share Posted August 30, 2007 Thanks pocobueno1388. Quote Link to comment https://forums.phpfreaks.com/topic/67365-solved-remove-whitespace/#findComment-337990 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.