Error removing accents from a string for creating friendly Urls

Asked

Viewed 506 times

2

Ola would like to know how I can get around the mistake that this being caused in the code below. When I accentuate special characters they are being removed instead of converted.

I caught this code with a friend already a while ago and suddenly began to present this error on several hosts hosting I do not know if it was because of the update of PHP more in any case I would like to know why this presenting this error.

The error is literally taking out the special characters and accents and putting in their place the value of the variable $slug is if the value of the $slug for a dash strip to accents and special characters and put dash in place instead of convert.

Example working before:

Testing text conversion to user friendly URL 1 = testing-conversational-text-to-url-friendly-1

Action, Comedy = action-comedy

Example of the current error:

Testing text conversion to user-friendly URL 1 = testing-convers-o-text-to-url-amig-vel-1

Action, Comedy = a-o-comdia

Literally the characters that were being converted are being deleted as I can fix it ?

   function removeAcentos($string, $slug = false) {
      $string = strtolower($string);

      // Código ASCII das vogais
      $ascii['a'] = range(224, 230);
      $ascii['e'] = range(232, 235);
      $ascii['i'] = range(236, 239);
      $ascii['o'] = array_merge(range(242, 246), array(240, 248));
      $ascii['u'] = range(249, 252);

      // Código ASCII dos outros caracteres
      $ascii['b'] = array(223);
      $ascii['c'] = array(231);
      $ascii['d'] = array(208);
      $ascii['n'] = array(241);
      $ascii['y'] = array(253, 255);

      foreach ($ascii as $key=>$item) {
        $acentos = '';
        foreach ($item AS $codigo) $acentos .= chr($codigo);
        $troca[$key] = '/['.$acentos.']/i';
      }

      $string = preg_replace(array_values($troca), array_keys($troca), $string);

      // Slug?
      if ($slug) {
        // Troca tudo que não for letra ou número por um caractere ($slug)
        $string = preg_replace('/[^a-z0-9]/i', $slug, $string);
        // Tira os caracteres ($slug) repetidos
        $string = preg_replace('/' . $slug . '{2,}/i', $slug, $string);
        $string = trim($string, $slug);
      }

      return $string;
    }
echo removeAcentos(Ação, Comédia, Ficção Cientifica, '-');

Example of Working Code to show error click here

  • I corrected my answer. Now I think it solves your problem.;

1 answer

5


You just want to convert the accents to ordinary letters?

Use the function iconv.

So do it like this:

echo iconv('utf-8', 'us-ascii//TRANSLIT', 'Pêra maçã côco'); 

Exit:

Coconut maca

See working on IDEONE

  • Works cool more type only converts texts need that in case do conversion of any character type or symbol also placing - between spaces. More in any case grateful for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.