划分空格+逗号[重复]

I know that if I would for example use this code:

<?php
    $tags = "hello, bye, test";
    $tags = explode(",", $tags);
?>

It would seperate by commas. But I would still have spaces left there and I want to save those values in the database without those spaces. How do I do this?

</div>

If the spaces are always present a quick solution is to use:

$tags = "hello, bye, test";
$tags = explode(", ", $tags);

Otherwise (as Michael suggested) you can trim the values:

$tags = "hello, bye, test";
$tags = explode(",", $tags);
$tags = array_map('trim', $tags);

EDIT: As Michael pointed out, we dont want to replace all whitespace, only that following a comma. We can use array map to run each item in the exploded array through trim.

<?php
    $tags = "hello, bye, test";
    $tags = array_map( 
        function($s){ 
            return trim($s); 
        }, 
        explode(",", $tags)
    );
?>

What about using explode with ", " as delimiter?

$tags = "hello, bye, test";
$atags = explode(", ", $tags);

Gets this:

print_r($atags);
Array ( [0] => hello [1] => bye [2] => test )

I know it is not the most beautiful solution, but it also works.

Edit
From the comment "Yes but it should work on both ways like the tag system here on stackoverflow. For example it should split "hello, bye" as well as "hello,bye" and "hello bye" – Xegano 1 min ago",

In this case, you should only use explode with "," and then trim whitespaces surrounding the words. Something like this:

$tags = "hello, bye, test";
$atags = explode(",", $tags);
$atags = array_map('trim', $atags);