Solution to iconv truncation in php

Source: Internet
Author: User
Tags apache log
This article mainly introduces how to solve the iconv Chinese truncation problem in php, and analyzes in detail the causes and specific solutions of the Chinese truncation problem in the form of examples, which has some reference value, for more information about how to solve iconv truncation in php, see the following example. Share it with you for your reference. The specific analysis is as follows:

Today, I made a collection program. The principle is very simple. I used the curl method to obtain and analyze the html of the target page, and then extracted the required data using regular expressions and saved it in the database.

Because the opposite side of the page is GB2312 encoding, while the local use of UTF-8 encoding. Therefore, encoding conversion is required after collection.

The iconv method is used for encoding conversion.

Iconv-string conversion based on the required character encoding
String iconv (string $ in_charset, string $ out_charset, string $ str)

Encodes str from in_charset to out_charset.

The conversion method is simple. you can simply use the iconv method.

<? Php $ content = iconv ('gb2312', 'utf-8', $ content); // $ content is the collected content?>

After testing several pages, the data can be collected normally. However, in subsequent collection, the collection of several pages is incomplete.
Check the backend servers for regular expressions. After investigation, it is found that the content after iconv transcoding is much less than the collected content.
View the apache log and see the prompt: Notice: iconv (): Detected an illegal character in input string.

Refer to the manual and see the following description.

If you add a string // Transcoder after out_charset, the transliteration function is enabled. This means that when a character cannot be expressed by the target character set, it can be expressed in an approximate manner by one or more character similar to the character set.

If you add a string // IGNORE, characters that cannot be expressed in the target character set will be discarded silently. Otherwise, str is truncated from the first invalid character and leads to an E_NOTICE.

When iconv encounters unrecognized content, it truncates the content from the first unrecognizable character and generates an E_NOTICE. Therefore, the following content is discarded.

When the output character set is appended with // IGNORE, only unrecognized content is discarded, and subsequent content is not truncated or discarded.

After the program is modified, everything works normally.

<? Php $ content = iconv ('gb2312', 'utf-8 // IGNORE ', $ content); // $ content is the collected content?>

Tips: when using iconv, if you want to use UTF-8 encoding, use UTF-8 instead of UTF8, because UTF8 has some server issues.

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.