Recover accents of a string with PHP

Asked

Viewed 89 times

3

I am using friendly urls and to generate the link, I am using this way:

$string = $jm->NomeCategoria; // retorna roupa de verão
$hifen = preg_replace('/[ -]+/' , '-' , $string); // incluo o hífen
$acentos = preg_replace( '/[`^~\'"]/', null, iconv( 'UTF-8', 'ASCII//TRANSLIT', $hifen)); // retiro as acentuações

With these commands he returns: summer wear. Only I need to recover on another page a string correct summer clothes, because I need to check the database. I was able to remove the hyphen and place the spaces, as it is possible to "return" the accents of a string?

  • You can not pass two variables ? the original and the result with hyphenation and without accents ?

  • Well... I also thought about creating in the database, when creating the categories, another field called url and when registering, generate the friendly link and then recover... but if you had a simpler way....

  • I think there’s no way, unless you create a dictionary for your categories, but it would end up turning into the same thing as having another field in the comic book

  • Because you do not save the variable "summer clothes" in a Session like this $_SESSION['url'] = $jm->NomeCategoria;

1 answer

4


Essentially there is no way. I would, but the solution is so complex that it doesn’t pay. There would have to be other mechanisms to solve this, keep the accents, have a code, have a table with and without accent, have various ways to solve.

Depending on what you need, you can search the database in an insensitive way, which is usually the most correct way, so it ignores the accents in the search. Not for all cases, but I find the most appropriate solution.

Browser other questions tagged

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