Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace comma or whitespace with hyphen in same string

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?

like image 484
Micku Avatar asked Nov 27 '22 17:11

Micku


1 Answers

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

Test:

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

echo $result;
//home-garden-gardener
like image 101
Esailija Avatar answered Dec 05 '22 17:12

Esailija