Detect encoding and make everything UTF-8

前端 未结 24 2783
暗喜
暗喜 2020-11-22 03:03

I\'m reading out lots of texts from various RSS feeds and inserting them into my database.

Of course, there are several different character encodings used in the fee

24条回答
  •  深忆病人
    2020-11-22 03:10

    I know this is an older question, but I figure a useful answer never hurts. I was having issues with my encoding between a desktop application, SQLite, and GET/POST variables. Some would be in UTF-8, some would be in ASCII, and basically everything would get screwed up when foreign characters got involved.

    Here is my solution. It scrubs your GET/POST/REQUEST (I omitted cookies, but you could add them if desired) on each page load before processing. It works well in a header. PHP will throw warnings if it can't detect the source encoding automatically, so these warnings are suppressed with @'s.

    //Convert everything in our vars to UTF-8 for playing nice with the database...
    //Use some auto detection here to help us not double-encode...
    //Suppress possible warnings with @'s for when encoding cannot be detected
    try
    {
        $process = array(&$_GET, &$_POST, &$_REQUEST);
        while (list($key, $val) = each($process)) {
            foreach ($val as $k => $v) {
                unset($process[$key][$k]);
                if (is_array($v)) {
                    $process[$key][@mb_convert_encoding($k,'UTF-8','auto')] = $v;
                    $process[] = &$process[$key][@mb_convert_encoding($k,'UTF-8','auto')];
                } else {
                    $process[$key][@mb_convert_encoding($k,'UTF-8','auto')] = @mb_convert_encoding($v,'UTF-8','auto');
                }
            }
        }
        unset($process);
    }
    catch(Exception $ex){}
    

提交回复
热议问题