我需要PHP代码用连字符
替换逗号或空格例如:
如果是$value = 'home garden'
或$value = 'home,garden'
,
我需要结果home-garden
我尝试了$result = preg_replace('/\s+[\,]/', '-', trim($value));
,但没有用..
有人可以解释一下吗?
答案 0 :(得分:12)
$result = preg_replace('/[ ,]+/', '-', trim($value));
测试:
$value = ' home ,garden , gardener ';
$result = preg_replace('/[ ,]+/', '-', trim($value));
echo $result;
//home-garden-gardener
答案 1 :(得分:9)
$result = str_replace(array(',', ' '), '-', $value);
答案 2 :(得分:1)
$stringAfter = str_replace(",", "-",$stringBefore);
example:
$stringBefore="abc,def,ghi";
$stringAfter = str_replace(",", "-",$stringBefore);
print $stringAfter;
Output:
`abc-def-ghi`