删除特殊字符并在单词php mysql之间添加短划线

I am trying to strip all special chars and add a dash between all the words.

Example:

Example text: How to make something, something else, and other stuff.

Example string:

$string = "How to make something, something else, and other stuff.";

Example of how it should look: How-to-make-something-something-else-and-other-stuff

I need a function that will strip all special chars but also add the dashes between every word "but also not adding dashes after the last word". Anyone have any ideas of how I should do this? I assume this is fairly simple. probably a preg_replace with regex which would solve the special char problem but adding the dashes is where I get confused and am not sure what to do.

Assuming the string is in utf-8:

$string = 'Höw to máke sòmething, something else, and other stuff';
$string = preg_replace('/[^a-z0-9]+/i','-',
   iconv('UTF-8','ASCII//TRANSLIT',$string));
$string = trim($string,'-');

After you've removed the non-alpha characters, you can explode() on spaces and then implode() with dashes.

I would explode the string at the spaces and then implode it with dashes like this:

$string = trim($string); // removes white space from ends
$string = preg_replace("/[^A-Za-z0-9]/","",$string); // removes special chars 
$string = explode(' ', $string); // separates the words where there are spaces
$string = implode('-', $string); // puts the words back into a sentence separated by dashes
echo $string;

this can be condensed obviously - but for simplicity these are the steps needed.