缩短此功能

I wrote this code to prepare a title for a link, but I think it is a bit bulky and am wondering if anyone with a better understanding of regex would be able to reduce the following function (by merging the relevant preg_replaces). I need it to strip all current hyphens, strip multiple spaces, ensure that it is solely alphanumeric apart from the space-replacing hyphen, replace all spaces with a single hyphen and ensure that the string doesn't start with a hyphen:

function prepareURLTitle($title)
{

return preg_replace("/\A-/", "", str_replace(" ", "-", preg_replace("/[^a-zA-Z0-9\s]/", "", preg_replace('/\s\s+/', ' ', preg_replace('/\s?-/', '', $title)))));

}

An example of input and it's output:

Input:

BRAND NEW - Gloves, 2 pack //Multiple spaces are in here but the blockquote won't allow me to display them

Output:

BRAND-NEW-Gloves-2-pack

function prepareURLTitle($title)
{
   return preg_replace( "/[^a-zA-Z0-9]/", "-",str_replace("-", "",  $title));
}

DEMO: http://codepad.org/lPSQQBys

OUTPUT:

BRAND-NEW--Gloves--2-pack

trim(preg_replace('`[^a-z0-9]+`i','-',str_replace("'",'',$title)),'-')

I also replaced quotes with nothing, so strings like "The cat's meow" don't become "The-cat-s-meow".

function prepareURLTitle($title)
{
   return preg_replace("[^A-Za-z0-9]+", "-", $title);
}

This should work. You need to replace multiple non-alphanumeric characters with a single "-".

preg_replace('~[^a-z\d]+~i','-',preg_replace('~^[^a-z\d]+(.*?)[^a-z\d]+$~i','$1',$title));
// or
preg_replace(array('~^[^a-z\d]+(.*?)[^a-z\d]+$~i','~[^a-z\d]+~i'),array('$1','-'),$title);

With an example…

$title = '  BRAND   NEW -   Gloves, 2 pack -  ';
echo preg_replace(array('~^[^a-z\d]+(.*?)[^a-z\d]+$~i','~[^a-z\d]+~i'),array('$1','-'),$title);

will return

BRAND-NEW-Gloves-2-pack