Make a safe url

Wednesday, March 17th, 2010

This is a function that checks a string and makes it work nicely for friendly urls

  1. // Make sure a url string is nicely formatted
  2. function makeSafeUrl($myUrl, $allowSpace=0, $allowCase=0, $allowDot=0){
  3.         $sSafe = 'abcdefghijklmnopqrstuvwxyz1234567890-_';
  4.         $disallowed = array();
  5.         $disallowed['c'] = 'ç';
  6.         $disallowed['n'] = 'ñ';
  7.         $disallowed['y'] = 'ýÿ';
  8.         $disallowed['e'] = 'èéêë';
  9.         $disallowed['a'] = 'àáâãäå';
  10.         $disallowed['o'] = 'ðóòôõöø';
  11.         $disallowed['u'] = 'ùúûü';
  12.         $disallowed['i'] = 'ìíîï';
  13.        
  14.         if(!$allowSpace) $disallowed['-'] = ' ';
  15.         if($allowSpace) $sSafe .= ' ';
  16.        
  17.         if(!$allowDot) $disallowed['-'] = '.';
  18.         if($allowDot) $sSafe .= '.';
  19.        
  20.         if($allowCase){
  21.                 $sSafe .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  22.                 $disallowed['C'] = 'Ç';
  23.                 $disallowed['N'] = 'Ñ';
  24.                 $disallowed['Y'] = 'ÝŸ';
  25.                 $disallowed['E'] = 'ÈÉÊË';
  26.                 $disallowed['A'] = 'ÀÁÂÃÄÅ';
  27.                 $disallowed['O'] = 'ÐÓÒÔÕÖØ';
  28.                 $disallowed['U'] = 'ÙÚÛÜ';
  29.                 $disallowed['I'] = 'ÌÍÎÏ';
  30.         }else{
  31.                 $myUrl = strtolower($myUrl);
  32.         }
  33.  
  34.         $newString = array();
  35.  
  36.         for($i = 0; $i<strlen($myUrl); $i++){
  37.                 $thisChar = $myUrl[$i];
  38.                 if(stristr($sSafe, $thisChar)){
  39.                         $newString[$i] = $thisChar;
  40.                 }else{
  41.                         foreach($disallowed as $key => $var){
  42.                                 if(stristr($var, $thisChar)){
  43.                                         $newString[$i] = $key;
  44.                                 }
  45.                         }
  46.                 }
  47.         }
  48.         return implode('', $newString);
  49. }

Display clean php code for copying

 // 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);
}
click here to close

Help keep these files free,
and support further development!