Convert any string to UTF-8 without knowing the original character set

12

I need a function or class to ensure that data sent from a form ( from anywhere in the world ) goes to the database in UTF-8 encoding.

I have tried, $string = iconv(mb_detect_encoding($text), "UTF-8", $text); but it has problems (if the entry is 'nation' it returns 'na').

I have tried $string = mb_convert_encoding($text, "UTF-8"); but also have problems return na

    
asked by anonymous 03.04.2017 / 13:23

1 answer

8

It has a library available that does this role without you having to be in doubt between which function of PHP to use to convert special character.

  

This library works only on PHP co_de >=

Click here to go to the repository in 5.3 .

Use as follows:

<?php
use \ForceUTF8\Encoding;

echo Encoding::toUTF8("nação");
# retorna: nação

echo Encoding::fixUTF8("nação");
# retorna: nação

[EDIT]

Or, as follows:

 <?php
 require_once "forceUTF8/Encoding.php";
 echo \ForceUTF8\Encoding::fixUTF8("nação");

Or

 <?php
 require_once "forceUTF8/Encoding.php";
 $utf8 = new Encoding;
 echo $utf8::fixUTF8("nação");
    
03.04.2017 / 13:43