Hi

I'm making a class to normalise a url for rewriting and I was wondering if there's an existing function that would replace accented letters with the non-accented equivalent

for example : "é", "è" and "ë" would be replaced with "e"

or would I need to set up some arrays to do this ?

Ideally I'd like to be able to use page titles in the url like in blogs but replacing special characters with non-special ones and replacing spaces and apostrophes with an underscore

    [man]iconv[/man]("your current character set", "ASCII//TRANSLIT", $string)

    If that doesn't work, look at the examples on the [man]strtr[/man] page.

      Hi

      thanks for your reply - I couldn't get iconv() to work so I went with

      function normaliseUrlString($str){
      
      $table = array(
      	'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
      	'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
      	'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
      	'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
      	'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
      	'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
      	'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
      	'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r', ' '=>'_', "'"=>'_', '/'=>''
      );   
        $str = strtr($str, $table);
      
      // get rid of any remaining unwanted characters
      $str = ereg_replace("[^A-Za-z0-9/-/_]", "", $str);
      
      // remove repeated underscores
      $str = preg_replace('/[_]+/', '_', $str);
      
      return $str; 
      }
      
      $str = "L'éléctricité est un d'émon &'(-_)=²~#{[|`\^@]}^$*!:;,¤¨£µ%§/.?<> c'est bon";
      
      echo normaliseUrlString($str);
      

      however, I had to put '/'=>'' in the $table because

      $str = ereg_replace("[A-Za-z0-9/-/_]", "", $str);

      didn't remove the slash - have you got any idea why could be ?

      thanks

        steamPunk;10944014 wrote:

        $str = ereg_replace("[A-Za-z0-9/-/_]", "", $str);

        didn't remove the slash - have you got any idea why could be ?

        Of course it didn't remove the slash.. you told it not to. The '' in the character class negates the class.

        Also, you shouldn't use the ereg*() functions as they've been deprecrated. Instead, use [man]preg_replace/man.

          bradgrafelman;10944018 wrote:

          Of course it didn't remove the slash.. you told it not to. The '' in the character class negates the class.

          oops - silly me - I'm not too well versed in regex and I thought that the /- was to escape the -

          bradgrafelman;10944018 wrote:

          Also, you shouldn't use the ereg*() functions as they've been deprecrated. Instead, use [man]preg_replace/man.

          I didn't know that - I've changed it now

          thanks 🙂

            To escape a character, you use a backslash. What /-/ does is create a range of characters from '/' to.. '/'. So in other words, it's the same as just writing '/' by itself.

              Write a Reply...