Jump to content

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


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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.