search
HomeBackend DevelopmentPHP Tutorial如何把数据从GB2312转换成UTF-8

我使用了iconv和mb_convert_encoding两个函数都试了,然后发现结果是一样的,都会出现各种问题,不是哪里少字了就是直接错误,请问大神怎么解决啊


回复讨论(解决方案)

iconv("GB2312","UTF-8//IGNORE",$data) 

确定 原数据是 GB2312编码吗

是的  确定了 是ANSI格式的

是的  确定了 是ANSI格式的
你的文件页面编码是什么? 要不把代码贴出来看看

是这样的 在做一个scv文件的导入功能 然后用记事本打开显示保存编码是ANSI,现在用fgetcvs来循环,在里面怎么弄都是不正常的。之前用记事本打开保存为utf-8导入功能是OK的 ,所以页面编码应该是utf-8

GB2312 是 GBK 的子集
UCS-2 是 GBK 的变体
UCS-2 与 UTF-8 间存在简单的位移操作

所以从 GB2312 或 GBK 转换到 UTF-8 不存在数据丢失的现象
反过来就有丢失的可能了

ANSI 不能说明任何问题
ANSI 泛指国家标准,计算机中指的是操作系统的默认字符集(可手工修改)
以 windows 为例,简体中文系统就是 GBK、繁体中文系统就是 BIG5

那帮忙看下好吗 我填写的cvs文件中地区的信息是湖北省,湖南省,广东省,通过iconv('GB2312','UTF-8',$str),就转成一个‘省’字,假如是山东省或者山西省是完全翻译,四川省翻译成‘拇ㄊ’,这个是什么原因啊 卡了2天了 

你能把你的文件放到云盘吗?
或是
echo base64_encode(file_get_contents('你的csv文件名', false, null, 0, 1000));
贴出结果

结果是这样的:


然后我用 echo iconv('GBK','UTF-8',file_get_contents($file['tmp_name']));return false;
得到的完全都是utf8的数据,可惜这样不是我想要的

1、贴出 base64 数据
2、说一下你打算做什么

把代码贴出来  看一下   简单的叙述你的需求

seDC6yxDUk2x4MLrLMP7s8YsvPKzxizQ0NK1LCK/zbunwODQzaOosOzKwrSmOjEs1+7W1b/Nu6c6Miy0+sDtycw6M6OpIiwiveHL48Dg0M2jqNfu1tW/zbunOjEsveHL47/Nu6c6MizP+srbv827pzozo6kiLMv5yvS/zbunLMv51NrH+NPyLMGqz7XIyyzBqs+1yMu157uwLLXY1rcssbjXoiyxuNeiMiyxuNeiMw0KLGZnZ2csc2RmZ2ZkaGgsc2FkLM28yum53dTEwMDK0iwzLDAsZmRnZGZnLMm9tqvKoSxhc2RmZywxMzgwMDEzODAwMCxmZGhmZ2hmLGFhYSxiYmIsY2NjDQo=
我想要的就是通过fgetcsv函数循环每行记录组成数组,存入数据库 但是直接file_get_contents的话拿到的想做到这种效果比较困难,因为在cvs文件里好多个字段,有些可填有些必填。所以才那么纠结

那根编码转换有什么关系

$fp = fopen('你的csv文件名', 'r');while($row = fgetcsv($fp)) {  $res[] = $row;}print_r($res);

谢谢版主大大这么晚了还在帮忙解决问题 ,是这样的 ,cvs文件的编码是gb2312的,数据库编码是utf8的,然后我装进数组之前要把这里获取到的数据和库里面的做比对,来获取我要的数据,而且cvs文件里好多字段的都是汉字,所以要转码才能入库,问题是在外面file_get_contents之后转码可以进去循环里面一个一个转码就不行。

不需要转码
只需在比较之前执行一次 mysql_query('set names gbk'); 就可以了

版主大大  再问一个问题啊 ,那我的gbk编码的数据如何入库啊,库里面是utf8的编码,我怀疑是不是fegtcvs这个函数的作用啊 ,再里面转码就不行在外面转码就可以

gbk 的数据插入到 utf8 的表,只需在插入前执行一次 mysql_query('set names gbk'); 就可以了

mysql_query(' set names gbk');
这条指令的作用是:通知 mysql 现在用 gbk 字符集进行操作
转码的工作由 mysql 自己完成

恩 谢谢版主大大 ,虽然说我换了file代替了fgetcsv实现了功能

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools