QueenZ Posted August 23, 2008 Share Posted August 23, 2008 Hi guys, Let's say that i have a string like this... $string = "word word word"; or like this $string = "word------word-----word"; I need them to be. $string = "word word word"; $string= "word-word-word" I'm wondering what function removes odd characters from the string. Characters that repeats... As far as i know trim() only removes repeated chars before or after the string only.. Quote Link to comment https://forums.phpfreaks.com/topic/121043-how-to-remove-odd-spaces/ Share on other sites More sharing options...
cooldude832 Posted August 23, 2008 Share Posted August 23, 2008 well if we assume any space >2 is bad we can try to do it with regex we can use a pattern like ^(\s\s+)?$ and use preg_replace to fix it up Quote Link to comment https://forums.phpfreaks.com/topic/121043-how-to-remove-odd-spaces/#findComment-623964 Share on other sites More sharing options...
QueenZ Posted August 23, 2008 Author Share Posted August 23, 2008 well if we assume any space >2 is bad we can try to do it with regex we can use a pattern like ^(\s\s+)?$ and use preg_replace to fix it up I'm not that good with this kind of stuff so i don't know how to do this.. Quote Link to comment https://forums.phpfreaks.com/topic/121043-how-to-remove-odd-spaces/#findComment-623969 Share on other sites More sharing options...
wildteen88 Posted August 24, 2008 Share Posted August 24, 2008 well if we assume any space >2 is bad we can try to do it with regex we can use a pattern like ^(\s\s+)?$ and use preg_replace to fix it up I'm not that good with this kind of stuff so i don't know how to do this.. $string = "word word word"; $new_string = preg_replace('/([\s]+)/', ' ', $string); echo '<pre>' . $string . '</pre>'; echo '<pre>' . $new_string . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/121043-how-to-remove-odd-spaces/#findComment-624183 Share on other sites More sharing options...
sasa Posted August 24, 2008 Share Posted August 24, 2008 or try <?php $string = "word-----word word 1+++2000???"; echo preg_replace('/([^\w])\1+/','\1',$string); ?> Quote Link to comment https://forums.phpfreaks.com/topic/121043-how-to-remove-odd-spaces/#findComment-624269 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.