jb60606 Posted June 2, 2007 Share Posted June 2, 2007 I have a file, containing stock symbols: symbols.dat IBM SIRI GOOG AMD INTC MSFT DELL I need to extract all symbols from the file, and turn them into a string, each being separated by a "+". IBM+SIRI+GOOG+AMD+INTC+MSFT+DELL I do this with the following code: $arr = file('data/symbols.dat'); //extract file contents into array $sym = implode('+', $arr); //make array a string, separate each array element with a "+" The end result is the string, though, there is a space after each symbol, before the "+" IBM +SIRI +GOOG +AMD +INTC +MSFT +DELL Does anyone know how I can get rid of the spaces? I've tried using str_replace, rtrim, etc, though either I'm using them in the wrong order, or they just doesn't work. The string is used in a URL, so there can be no spaces. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/53980-cant-remove-spaces-from-string-extracted-from-array/ Share on other sites More sharing options...
taith Posted June 2, 2007 Share Posted June 2, 2007 $arr = file('data/symbols.dat'); foreach($arr as $k=>$v){ $arr[$k]=trim($v); } $sym = implode('+', $arr); Quote Link to comment https://forums.phpfreaks.com/topic/53980-cant-remove-spaces-from-string-extracted-from-array/#findComment-266842 Share on other sites More sharing options...
jb60606 Posted June 2, 2007 Author Share Posted June 2, 2007 works perfectly! Thanks a million. Quote Link to comment https://forums.phpfreaks.com/topic/53980-cant-remove-spaces-from-string-extracted-from-array/#findComment-266850 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.