Jump to content

*SOLVED* how do you shave empty characters from begin/end of a string


bcamp1973

Recommended Posts

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 :(

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.