URL de tira de PHP de todos los caracteres inválidos

//replaces all invalid characters in a url with "-"
function url($url) {
   $url = preg_replace('~[^\\pL0-9_]+~u', '-', $url);
   $url = trim($url, "-");
   $url = iconv("utf-8", "us-ascii//TRANSLIT", $url);
   $url = strtolower($url);
   $url = preg_replace('~[^-a-z0-9_]+~', '', $url);
   return $url;
}
//usage:
$invalidURL = "https://example.com/~+test;
$validURL = url($invalidURL);
cobbsaladluv