Replace comma or whitespace with hyphen in same string

php, preg-replace, regex

Solution

$result = preg_replace('/[ ,]+/', '-', trim($value));

Test:

$value = '  home  ,garden , gardener  ';
$result = preg_replace('/[ ,]+/', '-', trim($value));

echo $result;
//home-garden-gardener

Problem

I need PHP code to replace comma or whitespace with hyphen For eg: If `$value = 'home garden'` or `$value = 'home,garden'` , i need result as `home-garden` I tried `$result = preg_replace('/\s+[\,]/', '-', trim($value));` , but no use.. Can someone explain it?

Original source