bcamp1973 Posted July 27, 2006 Share Posted July 27, 2006 ok, so i have a string...[code]$string='foo, bar,blue ,bear,bla bla';[/code]i want each element (separated by commas) in an array, so...[code]$string=explode(',',$string)[/code]so, now i have this...[code]array='foo',' bar','blue ','bear','bla bla'[/code]but, i don't want the extra white spaces at either end of any array element such as ' bar' or 'blue ', but i don't want to remove the space inside an element such as 'bla bla'. Is there a function to do this? RegEx skills are pathetic so I'm having trouble and str_replace(' ','',$string) obviously won't work :( Quote Link to comment https://forums.phpfreaks.com/topic/15823-solved-how-do-you-shave-empty-characters-from-beginend-of-a-string/ Share on other sites More sharing options...
trq Posted July 27, 2006 Share Posted July 27, 2006 [url=http://php.net/trim]trim[/url](). Quote Link to comment https://forums.phpfreaks.com/topic/15823-solved-how-do-you-shave-empty-characters-from-beginend-of-a-string/#findComment-64759 Share on other sites More sharing options...
trq Posted July 27, 2006 Share Posted July 27, 2006 Sorry... you might need to do something like...[code=php:0]$string=explode(',',$string)foreach($string as $val) { $arr[] = trim($val);}[/code]That will give you your array all cleaned up in $arr. Quote Link to comment https://forums.phpfreaks.com/topic/15823-solved-how-do-you-shave-empty-characters-from-beginend-of-a-string/#findComment-64762 Share on other sites More sharing options...
bcamp1973 Posted July 27, 2006 Author Share Posted July 27, 2006 Awesome! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/15823-solved-how-do-you-shave-empty-characters-from-beginend-of-a-string/#findComment-64763 Share on other sites More sharing options...
ryanlwh Posted July 27, 2006 Share Posted July 27, 2006 shorter version[code]$string=explode(',',$string);$arr = array_map('trim',$string);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15823-solved-how-do-you-shave-empty-characters-from-beginend-of-a-string/#findComment-64764 Share on other sites More sharing options...
xyph Posted July 27, 2006 Share Posted July 27, 2006 trim() will have to be done to each element of the array. Rexgex wil lbe much better.<?$string = 'foo, bar,blue ,bear,bla bla, omg , test';$replace = ',';$fixed = preg_replace('/[\s]*(,)[\s]*/i', $replace, $string);echo $fixed;?>Outputs : foo,bar,blue,bear,bla bla,omg,test Quote Link to comment https://forums.phpfreaks.com/topic/15823-solved-how-do-you-shave-empty-characters-from-beginend-of-a-string/#findComment-64770 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.