Use the function below to convert string to URL friendly slug. I used this function to create filename that can be send as attachments with emails becauase filenames with spaces do not mostly send properly.
Here we are:
- removing spaces
- converting uppercase letters to lowercase letters
- replacing accented characters by equivalent standard characters and a bit more.
function createSlug($str, $delimiter = '-'){
$slug = strtolower(trim(preg_replace('/[\s-]+/', $delimiter, preg_replace('/[^A-Za-z0-9-]+/', $delimiter, preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $str))))), $delimiter));
return $slug;
}