将句子分成用逗号分隔的单词

i have one sentence saved in database field , an i want to break it in words, but that words needs to be separated by coma, like this:

This, is, test, text

I tried something with explode() but it didnt do the work.

You could do

$text = "This is test text";
echo str_replace (" ", ", ", $text); // This, is, test, text

You can do a preg_split() to split words based on at least one space in between (though an exclamation mark with surrounding spaces would also be considered a word); afterwards you stick them back together.

echo join(', ', preg_split('/\s+/', $str));

Alternatively, let wordwrap do the work for you, decorate and undecorate:

echo join(', ', explode("\0", wordwrap($s, 1, "\0")));

I know it's not the most efficient way, but if you want to use explode, you'll need to use implode also:

<?php

$foo = 'this is a test';
$bar = implode(', ', explode(' ', $foo));
print_r($bar);

?>

would show : this, is, a, test