Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

ceejayoz http://ceejayoz.com/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Make string usable as a URI

This function will turn any string into a safe string usable in a URI.

   1  function dirify($s) {
   2       $s = convert_high_ascii($s);  ## convert high-ASCII chars to 7bit.
   3       $s = strtolower($s);           ## lower-case.
   4       $s = strip_tags($s);       ## remove HTML tags.
   5       $s = preg_replace('!&[^;\s]+;!','',$s);         ## remove HTML entities.
   6       $s = preg_replace('![^\w\s.]!','',$s);           ## remove non-word/space/period chars.
   7       $s = preg_replace('!\s+!','-',$s);               ## change space chars to dashes.
   8       return $s;    
   9  }
  10  
  11  function convert_high_ascii($s) {
  12   	$HighASCII = array(
  13   		"!\xc0!" => 'A',    # A`
  14   		"!\xe0!" => 'a',    # a`
  15   		"!\xc1!" => 'A',    # A'
  16   		"!\xe1!" => 'a',    # a'
  17   		"!\xc2!" => 'A',    # A^
  18   		"!\xe2!" => 'a',    # a^
  19   		"!\xc4!" => 'Ae',   # A:
  20   		"!\xe4!" => 'ae',   # a:
  21   		"!\xc3!" => 'A',    # A~
  22   		"!\xe3!" => 'a',    # a~
  23   		"!\xc8!" => 'E',    # E`
  24   		"!\xe8!" => 'e',    # e`
  25   		"!\xc9!" => 'E',    # E'
  26   		"!\xe9!" => 'e',    # e'
  27   		"!\xca!" => 'E',    # E^
  28   		"!\xea!" => 'e',    # e^
  29   		"!\xcb!" => 'Ee',   # E:
  30   		"!\xeb!" => 'ee',   # e:
  31   		"!\xcc!" => 'I',    # I`
  32   		"!\xec!" => 'i',    # i`
  33   		"!\xcd!" => 'I',    # I'
  34   		"!\xed!" => 'i',    # i'
  35   		"!\xce!" => 'I',    # I^
  36   		"!\xee!" => 'i',    # i^
  37   		"!\xcf!" => 'Ie',   # I:
  38   		"!\xef!" => 'ie',   # i:
  39   		"!\xd2!" => 'O',    # O`
  40   		"!\xf2!" => 'o',    # o`
  41   		"!\xd3!" => 'O',    # O'
  42   		"!\xf3!" => 'o',    # o'
  43   		"!\xd4!" => 'O',    # O^
  44   		"!\xf4!" => 'o',    # o^
  45   		"!\xd6!" => 'Oe',   # O:
  46   		"!\xf6!" => 'oe',   # o:
  47   		"!\xd5!" => 'O',    # O~
  48   		"!\xf5!" => 'o',    # o~
  49   		"!\xd8!" => 'Oe',   # O/
  50   		"!\xf8!" => 'oe',   # o/
  51   		"!\xd9!" => 'U',    # U`
  52   		"!\xf9!" => 'u',    # u`
  53   		"!\xda!" => 'U',    # U'
  54   		"!\xfa!" => 'u',    # u'
  55   		"!\xdb!" => 'U',    # U^
  56   		"!\xfb!" => 'u',    # u^
  57   		"!\xdc!" => 'Ue',   # U:
  58   		"!\xfc!" => 'ue',   # u:
  59   		"!\xc7!" => 'C',    # ,C
  60   		"!\xe7!" => 'c',    # ,c
  61   		"!\xd1!" => 'N',    # N~
  62   		"!\xf1!" => 'n',    # n~
  63   		"!\xdf!" => 'ss'
  64   	);
  65   	$find = array_keys($HighASCII);
  66   	$replace = array_values($HighASCII);
  67   	$s = preg_replace($find,$replace,$s);
  68       return $s;
  69  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS