Jump to content

Removing commas from a string and keeping it a string?


brian914

Recommended Posts

I have the following function, which takes a string with commas in it and attempts remove those commas. The way I have it here is that I use explode to take out the commas, which makes an array, and then iterate through that array to put a string back together without the commas.

 

function tags_to_sort_by( $sortMeta ) {

$sortByArray = explode(",", $sortMeta);

$sortByCount = count($sortByArray);

for ($i = 0; $i < $sortByCount; $i++) {

$sortByString += $sortByArray[$i];

}

return $sortByString;

}

 

What does not work is the following line:

$sortByString += $sortByArray[$i];

Somehow that outputs a 0, which I don't understand. It should output something like: arrayItem1 arrayItem2 array3 etc.

 

My question is if there either is an easier way to remove the commas from the original string or what I am doing wrong in this line:

$sortByString += $sortByArray[$i];  // I am trying to concatenate each part of the array back into the string.

 

Thanks a lot for help with this!

Link to comment
Share on other sites

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.