Re: strip spaces from inside string

From: Date: Fri, 05 Apr 2002 09:53:15 +0000
Subject: Re: strip spaces from inside string
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-91670@lists.php.net to get a copy of this message
the easiest way would be: <? // ------------- // init var $str = ""; $str = "abc def ghi"; $str = str_replace (" ", "", $str); echo $str."<br>"; // ---------------------------- // output: // abcdefghi // ---------------------------- // // or a regex: $str = "abc def ghi"; $str = ereg_replace(" ", "", $str); echo $str."<br>"; //if you want to strip any whitespace character: $str = "abc def ghi"; $str = preg_replace("/\s/", "", $str); echo $str."<br>"; // ------------- ?> preg_replace is quite powerful, so if you only want to strip some spaces, you should prefer str_replace. Always think about how many power you need. str_replace -> ereg_replace -> preg_replace. Michael "Chris Boget" <chris@wild.net> schrieb im Newsbeitrag news:007f01c1d502$b70b3360$8c01a8c0@wild.net... > > Say I have a string "xyz abc def" and I want to remove all of the spaces > > withing the string to create a new one. What's the best way? > > <? > > $string = "xyz abc def"; > > $stringWithOutSpaces = implode( "", explode( " ", $string )); > > ?> > > Or you can use eregi_replace(), but I don't know what the regex would be. > I *think* you can do: > > <? > > $string = "xyz abc def"; > > $stringWithOutSpaces = eregi_replace( " ", "", $string ); > > ?> > > > Chris >

« previous php.general (#91670) next »