Re: Removing Spaces from Array Values

From: Date: Tue, 03 Jul 2007 17:47:21 +0000
Subject: Re: Removing Spaces from Array Values
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-258159@lists.php.net to get a copy of this message
kvigor wrote:
Need to remove all spaces from Array Values... And this doesn't work. Are we talking spaces ' ' or all white space? This may include a tab, space, new line, etc...
This is similar info that's within array values: $someArray[0] = "PHP is
awesome";    s/b PHPisawesome
This is similar info that's within array values: $someArray[1] = "The Toy Boat";        s/b TheToyBoat
Begin Code=================================== $num = count($someArray); for($num = 0; $cntr < $num; $cntr++) { str_replace(' ','',$someArray[$num]); echo "$someArray[$num]<br />"; } End Code===================================
This should do for you <plaintext><?php $someArray = array(); $someArray[] = "somethig specia to take apart"; $someArray[] = "somethig specia to take apart"; $someArray[] = "somethig specia to take apart "; print_r($someArray); foreach ( $someArray AS $k => $v ) { $someArray[$k] = preg_replace('!\s!', '', $v); // Removes white space ' ', \n, \r\n, etc... $someArray[$k] = str_replace(' ', '', $v); // Removes only spaces } print_r($someArray); -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare

« previous php.general (#258159) next »