preg_replace is cutting letters with accent

1
$slug = preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($_POST["titulo"])));

Example:

I'm sending: Esse é um titulo de uma página

It returns: esse-um-titulo-de-uma-p-gina

As you can see, you are cutting the letters with an accent.

How should it be: esse-e-um-titulo-de-uma-pagina . Where is the problem?

    
asked by anonymous 05.10.2018 / 18:44

2 answers

5

Your range in [...] is a-z and 0-9 , a to z only consider the letters of the "alphabet" and not its variations, ally work with accents in slug URLs from my point of view is a bad idea, it would be more interesting to associate the slug with an id and url to be merely illustrative and without accents, which can be solved with functions like iconv of PHP, but that is my opinion.

Modern browsers today believe that they work with UTF-8 in their urls, so maybe using the \u00C0-\u00FF range will work (as in renan's answer: link ):

$slug = preg_replace('/[^a-z0-9\u00C0-\u00FF]+/ui', '-', trim(strtolower($_POST["titulo"])));
  

Some old browsers do not work with Unicode, so maybe the best thing to do is to use the first suggestion without accents, but that's a story beyond, maybe in another question I'll detail how to do something like this

You can even use the @bfavaretto response: link must have in mind that the accents may not work, because if the .php document is saved with ANSI or iso-8859-1 / windows-1252 then it will fail with certainty, do you still want to use it as in the response of the @bfavaretto I recommend that you first read this answer calmly:

After reading and understanding how to use utf-8 properly you can try this:

$slug = preg_replace('/[^a-z0-9áàâãéèêíïóôõöúçñ]+/i', '-', trim(strtolower($_POST["titulo"])));
    
05.10.2018 / 19:00
-3

preg_replace : Performs a search for a regular expression and replaces it.

So basically, what you're going through is '/[^a-z0-9]+/i' everything that is not within that scope, replaces it with '-' .

The correct would be '/[^a-zà-ú0-9]+/i' :

$slug = preg_replace('/[^a-zà-ú0-9]+/i', '-', trim(strtolower($_POST["titulo"])));
    
05.10.2018 / 19:00