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