在同一字符串中用连字符替换逗号或空格 [英] Replace comma or whitespace with hyphen in same string
本文介绍了在同一字符串中用连字符替换逗号或空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要 PHP 代码来用连字符替换逗号或空格
I need PHP code to replace comma or whitespace with hyphen
例如:如果 $value = 'home Garden'
或 $value = 'home,garden'
,
For eg:
If $value = 'home garden'
or $value = 'home,garden'
,
我需要结果为home-garden
我试过 $result = preg_replace('/\s+[\,]/', '-', trim($value));
,但没有用..
I tried $result = preg_replace('/\s+[\,]/', '-', trim($value));
, but no use..
谁能解释一下?
推荐答案
$result = preg_replace('/[ ,]+/', '-', trim($value));
测试:
$value = ' home ,garden , gardener ';
$result = preg_replace('/[ ,]+/', '-', trim($value));
echo $result;
//home-garden-gardener
这篇关于在同一字符串中用连字符替换逗号或空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文