Re: Remove Spaces from Middle of String

From: Date: Tue, 03 Oct 2006 18:30:23 +0000
Subject: Re: Remove Spaces from Middle of String
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-242571@lists.php.net to get a copy of this message
>>> sender: "Kevin Murphy" date: "Tue, Oct 03, 2006 at 11:08:23AM >>> -0700" <<<EOQ > This works, but I was wondering if this was the best way to > accomplish this or not. I'm trying to remove any extra spaces (not > whitespace like carriage returns) from the middle of a string.... > mainly for times where people put extra spaces after periods in > paragraph. > > while(preg_match('/ /',$data)) > { $data = str_replace(" "," ",$data); } > > Is there a better way to accomplish this same task? (PHP 4.x). Thanks. Sure: $ cat test.php <?=ereg_replace(' {2,}', ' ', "A string with many spaces inside")?> $ php -f test.php A string with many spaces inside Cheers, Alex

« previous php.general (#242571) next »