Welcome Guest, Not a member yet? Register   Sign In
Error inserting special characters (spanish) into database
#4

[eluser]vitoco[/eluser]
Hi , if you set the charset to UTF8 in the table , you must first take this steps :

1.- save the .php file in UTF8
2.- encode de string to utf8 with utf8_encode

i have a helper with a function that encode the string only if it isn't utf8 ( utf8_encode_safe )

Code:
define("UTF_8", 1);
    define("ASCII", 2);
    define("ISO_8859_1", 3);

    function codificacion($texto)
    {
        $c        = 0;
        $ascii    = true;
        //
        for  ($i = 0 ; $i < strlen( $texto ) ; $i++ )
        {
            $byte = ord( $texto[$i] );
            //
            if( $c > 0 )
            {
                if( ( $byte>>6 ) != 0x2)
                {
                    return ISO_8859_1;
                }
                else
                {
                    $c--;
                }
            }
            elseif ($byte&0x80;)
            {
                $ascii = false;
                //
                if (($byte>>5) == 0x6)
                {
                    $c = 1;
                }
                elseif (($byte>>4) == 0xE)

                {
                    $c = 2;
                }
                elseif (($byte>>3) == 0x1E )
                {
                    $c = 3;
                }
                else
                {
                    return ISO_8859_1;
                }
            }
        }
        //
        return ($ascii) ? ASCII : UTF_8;
    }

    function utf8_encode_safe($texto)
    {
        return ( codificacion($texto) == ISO_8859_1) ? utf8_encode($texto) : $texto ;
    }

Saludos

NOTA : si eres hispanoparlante, te puedo dar una mejor explicacion , puesto que el ingles no es mi lengua materna


Messages In This Thread
Error inserting special characters (spanish) into database - by El Forum - 03-12-2009, 06:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB