修剪连续的重复字符

Kok*_*kos 4 php regex

所以我通过执行以下操作为我的页面创建URL:

$uri = strtolower($info->name);
$uri = str_replace('&','and',$uri);

$uri = $info->id."-".preg_replace('/[^a-zA-Z0-9]/','-',$uri);
Run Code Online (Sandbox Code Playgroud)

基本上我做的一切小写,改变&and和更改所有特殊字符的-.我唯一的问题,如果现在比如$info->namethis is - a string它会显示为this-is---a-string.

我希望这可以成为this-is-a-string没有做类似的事情str_replace('---','-',$input);

我想我需要一个正则表达式,但我对那些很可怕,所以我想知道是否有人可以帮助我.

Dog*_*ert 8

更改

$uri = $info->id."-".preg_replace('/[^a-zA-Z0-9]/','-',$uri);
Run Code Online (Sandbox Code Playgroud)

$uri = $info->id."-".preg_replace('/[^a-zA-Z0-9]+/','-',$uri);
Run Code Online (Sandbox Code Playgroud)