natalieG Posted July 9, 2007 Share Posted July 9, 2007 We have some blocks of text that have paragraphs and then sometoimes there are long stretches of blanks between words. How do we set up a pattern match so that it will remove all the blanks after the first?, that is, if I have 10 blank spaces, I want to remove nine blanks. Thanks, Natalie Quote Link to comment https://forums.phpfreaks.com/topic/59150-text-formatting/ Share on other sites More sharing options...
no_one Posted July 9, 2007 Share Posted July 9, 2007 Something like this should work. <?php function str_multspace_del($str) { $cnt = 0; do { $str = str_replace(' ',' ', $str, $cnt); } while( $cnt ); return $str; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/59150-text-formatting/#findComment-293758 Share on other sites More sharing options...
soycharliente Posted July 9, 2007 Share Posted July 9, 2007 If it's only text and spaces, this might work. <?php $string = "hello world 4 3 56 hi"; $explode = explode(" ", $string); $implode = implode(" ", $explode); echo $implode; //outputs "hello world 4 3 56 hi" ?> That will just make a list of everything between the space characters (whereby deleting all of them) and then reforming the entire string so that all elements have one space between them. Quote Link to comment https://forums.phpfreaks.com/topic/59150-text-formatting/#findComment-293768 Share on other sites More sharing options...
no_one Posted July 9, 2007 Share Posted July 9, 2007 @Charlieholder Tested that before posting, mine ran faster (~194-200% faster.. 2x). Also, afterward, the modified string still contained multiple spaces. Oh, if php version < 5.0.0 <?php function str_multspace_del($str) { $cnt = 0; do { $ls = strlen($str); $str = str_replace(' ', ' ', $str); } while( $ls != strlen($str) ); return $str; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/59150-text-formatting/#findComment-293783 Share on other sites More sharing options...
sasa Posted July 9, 2007 Share Posted July 9, 2007 try <?php $string = "hello world 4 3 56 hi"; echo $string1 = preg_replace('/[\s]+/',' ',$string); //remove spaces, tabs new line //or echo "<br />\n"; echo $string2 = preg_replace('/[ ]+/',' ',$string); // remove just spaces ?> Quote Link to comment https://forums.phpfreaks.com/topic/59150-text-formatting/#findComment-293810 Share on other sites More sharing options...
soycharliente Posted July 10, 2007 Share Posted July 10, 2007 @no_one: I only offered an alternative. A simple suggestion. I didn't run any regression tests on it before I posted it. BUT, thank you for teaching me. Quote Link to comment https://forums.phpfreaks.com/topic/59150-text-formatting/#findComment-293989 Share on other sites More sharing options...
no_one Posted July 10, 2007 Share Posted July 10, 2007 Yeah, didn't mean anything by it :S Just thought Nat would like to know before trying any of the suggestions given, in case they ran into problems. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/59150-text-formatting/#findComment-293993 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.