php使用iconv中文截断问题的解决方法
这篇文章主要介绍了php使用iconv中文截断问题的解决方法,以实例形式较为详细的分析了出现中文截断问题的原因与具体解决方法,具有一定参考借鉴价值,需要的朋友可
本文实例讲述了php使用iconv中文截断问题的解决方法。分享给大家供大家参考。具体分析如下:
今天做了一个采集程序,原理很简单,使用curl方法把对方页面的html获取分析,然后正则提取需要的数据并保存在数据库。
由于对方页面是GB2312编码,而本地使用的是UTF-8编码。因此在采集后需要进行编码转换。
使用了iconv方法进行编码转换
iconv — 字符串按要求的字符编码来转换
string iconv ( string $in_charset , string $out_charset , string $str )
将字符串 str 从 in_charset 转换编码到 out_charset 。
转换的方法很简单,直接使用iconv方法就可以了
试验了几个页面,,都能正常采集。但在之后的采集中,有几个页面采集不完整。
一开始考虑是否正则有错,检查后排除此问题。经过排查,发现经过iconv转码后的内容比采集的内容少了一大段。
查看apache log,看到提示:Notice: iconv(): Detected an illegal character in input string。
翻查手册,看到以下说明
如果你在 out_charset 后添加了字符串 //TRANSLIT,将启用转写(transliteration)功能。这个意思是,当一个字符不能被目标字符集所表示时,它可以通过一个或多个形似的字符来近似表达。
如果你添加了字符串 //IGNORE,不能以目标字符集表达的字符将被默默丢弃。 否则, str 从第一个无效字符开始截断并导致一个 E_NOTICE 。
原来iconv遇到不能识别的内容,会从第一个不能识别的字符开始截断,并生成一个E_NOTICE。因此后边的内容被丢弃了。
而在输出字符集后加上//IGNORE则只丢弃不能识别的内容,而不会截断和丢弃后面的内容。
修改程序后一切正常
Tips:使用iconv时,如果要使用UTF-8编码的,请使用UTF-8而不要使用UTF8,因为UTF8有些服务器会有问题。
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

The... (splat) operator in PHP is used to unpack function parameters and arrays, improving code simplicity and efficiency. 1) Function parameter unpacking: Pass the array element as a parameter to the function. 2) Array unpacking: Unpack an array into another array or as a function parameter.

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

How to obtain dynamic data of 58.com work page while crawling? When crawling a work page of 58.com using crawler tools, you may encounter this...

Why do negative margins not take effect in some cases? During programming, negative margins in CSS (negative...

How to solve the display problem caused by user agent style sheets? When using the Edge browser, a div element in the project cannot be displayed. After checking, I posted...

Why do negative margins not take effect in some cases? When using CSS to layout web pages, you often encounter negative margins (negative...

Discussing the reasons for misalignment of two inline-block elements. In front-end development, we often encounter element typesetting problems, especially when using inline-block...
