I used from Codeigniter (url_title) method that get a string and seprate it's word with a separator and return new string.
public static function generateSlug($str, $separator = '-', $lowercase = FALSE)
{
if ($separator === 'dash') {
$separator = '-';
} elseif ($separator === 'underscore') {
$separator = '_';
}
$q_separator = preg_quote($separator, '#');
$trans = array(
'&.+?;' => '',
'[^\w\d _-]' => '',
'\s+' => $separator,
'(' . $q_separator . ')+' => $separator
);
$str = strip_tags($str);
foreach ($trans as $key => $val) {
$str = preg_replace('#' . $key . '#i' . (true ? 'u' : ''), $val, $str);
}
if ($lowercase === TRUE) {
$str = strtolower($str);
}
return trim(trim($str, $separator));
}