Make safe url

17 Ⅴ 2010

This is a tiny function that makes a random string of predefined length.
It does not use letters and numbers that could be confusing, like O and 0.

 // Make sure a url string is nicely formatted
function makeSafeUrl($myUrl, $allowSpace=0, $allowCase=0, $allowDot=0){
	$sSafe = 'abcdefghijklmnopqrstuvwxyz1234567890-_';
	$disallowed = array();
	$disallowed['c'] = '';
	$disallowed['n'] = '';
	$disallowed['y'] = '';
	$disallowed['e'] = '';
	$disallowed['a'] = '';
	$disallowed['o'] = '';
	$disallowed['u'] = '';
	$disallowed['i'] = '';
	
	if(!$allowSpace) $disallowed['-'] = ' ';
	if($allowSpace) $sSafe .= ' ';
	
	if(!$allowDot) $disallowed['-'] = '.';
	if($allowDot) $sSafe .= '.';
	
	if($allowCase){
		$sSafe .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$disallowed['C'] = '';
		$disallowed['N'] = '';
		$disallowed['Y'] = 'ݟ';
		$disallowed['E'] = '';
		$disallowed['A'] = '';
		$disallowed['O'] = '';
		$disallowed['U'] = '';
		$disallowed['I'] = '';
	}else{
		$myUrl = strtolower($myUrl);
	}
 
	$newString = array();
 
	for($i = 0; $i $var){
				if(stristr($var, $thisChar)){
					$newString[$i] = $key;
				}
			}
		}
	}
	return implode('', $newString);
}