So I'm creating urls for my pages by doing the following:
$uri = strtolower($info->name);
$uri = str_replace('&','and',$uri);
$uri = $info->id."-".preg_replace('/[^a-zA-Z0-9]/','-',$uri);
Basically I make everything lowercase, change &
to and
and change all special characters to a -
. My only problem now if for instance $info->name
is this is - a string
it will show up as this-is---a-string
.
I would like this to become this-is-a-string
without doing something like str_replace('---','-',$input);
I figure I need a regular expression for this, but I'm horrible at those so I was wondering if someone can help me out.
Change
$uri = $info->id."-".preg_replace('/[^a-zA-Z0-9]/','-',$uri);
to
$uri = $info->id."-".preg_replace('/[^a-zA-Z0-9]+/','-',$uri);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With