Mb_convert_encoding

  • 2020-03-31 20:05:09
  • OfStack

Mbconvertencoding (PHP 4 = 4.0.6, PHP 5, PHP 7). Python script for converting text files between encodings (stable) - goerz/convertencoding.py. PHP mbdetectencoding - 30 examples found. These are the top rated real world PHP examples of mbdetectencoding extracted from open source projects. You can rate examples to help us improve the quality of examples.

It is necessary to use UTF-8 encoding for all texts in PhpSpreadsheet. If the script uses different encoding then you can convert those texts with PHP's iconv or mbconvertencoding functions. Fatal error: Allowed memory size of xxx bytes exhausted (tried to allocate yyy bytes) in zzz on line aaa. Parameters string. The string or array being encoded. The type of encoding that string is being converted to. Is specified by character code names before conversion. It is either an array, or a comma separated enumerated list.If fromencoding is not specified, the internal encoding will be used. See supported encodings.

However, English generally does not have coding problems, only Chinese data will have this problem. For example, when you write a program in Zend Studio or Editplus, you use GBK code. If the data needs to be entered into the database, and the database code is utf8, then you need to encode and convert the data.
For usage of mb_convert_encoding see official:
(link: http://cn.php.net/manual/zh/function.mb-convert-encoding.php)
Make a GBK To utf-8
Another GB2312 To Big5
However, to use the above function you need to install it but you need to enable the mbstring extension library first.
Iconv, another function in PHP, is also used to convert string encodings, similar to the previous function.
Here are some more detailed examples:
Iconv - Convert string to requested character encoding
(PHP 4 > = 4.0.5, PHP 5)
Mb_convert_encoding -- Convert character encoding
(PHP 4 > = 4.0.6, PHP 5)
Usage:
String mb_convert_encoding (string STR, string to_encoding [, mixed from_encoding])
You need to enable the mbstring extension library in php.ini; Extension =php_mbstring.dll; To get rid of
Mb_convert_encoding can specify multiple input encodings, which are automatically recognized based on the content, but the execution efficiency is much worse than that of iconv.
String iconv (string in_charset, string out_charset, string STR)
Note: for the second parameter, in addition to specifying the encoding to be converted, two suffixes can be added: //TRANSLIT and //IGNORE, where //TRANSLIT will automatically change the characters that cannot be directly converted into one or more similar characters, //IGNORE will IGNORE the characters that cannot be converted, and the default effect is to truncate from the first illegal character.
Returns the converted string, or FALSE on failure.
Use:
It was found that iconv made an error when converting the character '--' to gb2312. Without the ignore parameter, all strings following the character cannot be saved. Either way, the '-' is not converted successfully and cannot be output. In addition, mb_convert_encoding does not have this bug.
Generally, iconv is used, and mb_convert_encoding function is used only when the original encoding cannot be determined or the iconv cannot be displayed properly after conversion.
From_encoding is specified by a character code name before conversion. It can be an array or string - comma separated enumerated list. If it is not specified, the internal encoding will be 2.
$STR = mb_convert_encoding($STR, 'ucs-2le', 'JIS, eucjp-win, sjis-win');
$STR = mb_convert_encoding($STR, 'euc-jp', 'auto');
Example:
A little trap for using mb_convert_encoding transcoding in PHP
The use of mb_convert_encoding() method for character encoding conversion in PHP programs is familiar to all of you and is widely used. And in general, the method is good enough to merit praise. But in a project where we needed to use it to convert from UTF8 to GBK, we found a modest problem converting some special characters. Specifically, MB converts characters encoded in utf8 but not encoded in GBK to 0x000x80, which causes problems with converted GBK characters.
In our consciousness, in the process of character encoding conversion, if the target encoding is not expressible characters, transcoding program should do is to discard such characters, so that although some data is lost, but will not cause the transcoding of the character sequence is not available. It is not clear why MB should use the above method rather than discard it.
The temporary solution is to filter the transcoding sequence of strings, filtering out all characters of x0080. Or filter utf8 strings before escaping, filtering out all characters that ut8 can represent but GBK cannot. In terms of implementation difficulty, the first filtering method is relatively easy to do.

'mbstring' Extension and Non-ASCII Encoding Management

mb_convert_encoding() and Other mbstring Functions

This section describes some basic functions provided in the mbstring extension library. mb_strlen() returns the number of characters. mb_convert_encoding() converts a string to a new encoding schema.

Let's first look at some of the basic 'mbstring' functions offered in the 'mbstring' extension library:

mb_get_info ( ) - Returns the current settings of 'mbstring' extension.

mb_internal_encoding ( [string encoding] ) - Sets the current internal encoding with the specified encoding name. If no encoding is specified, it returns the current internal encoding.

string mb_strlen ( string str [, string encoding] ) - Returns the number of characters in the specified string based on the specified encoding name. If no encoding is specified, it uses the current internal encoding.

string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] ) - Converts the specified string to a new encoding from an old encoding and returns the converted string. If no old encoding is specified, it uses the current internal encoding.

mb_detect_encoding ( string str ) - Detects and returns the encoding name of the specified string.

mb_http_input ( string type ) - Detects and returns the encoding name of the specified HTTP input type: 'G' for GET, 'P' for POST, 'C' for COOKIE.

mb_http_output ( [string encoding] ) - Sets or returns the current HTTP output encoding.

mb_output_handler ( string contents, int status ) - Call back function for output buffer. When used as ob_start('mb_output_handler'), all strings going to the output buffer will be converted from the internal encoding to the HTTP output encoding.

mb_parse_str ( string encoded_string [, array &result] ) - Parses URL encoded strings, like the query string received with GET method. Resulting variables will be stored in the specified array. If no array is specified, resulting variables will be promoted into global variables.

Last update: 2019.

Table of Contents

About This Book

Introduction and Installation of PHP 7.3

PHP Script File Syntax

PHP Data Types and Data Literals

Mb_convert_encoding Html_entities

Variables, References, and Constants

Expressions, Operations and Type Conversions

Conditional Statements - 'if' and 'switch'

Loop Statements - 'while', 'for', and 'do .. while'

Function Declaration, Arguments, and Return Values

Arrays - Ordered Maps

Introduction of Class and Object

Integrating PHP with Apache Web Server

Retrieving Information from HTTP Requests

Creating and Managing Sessions in PHP Scripts

Sending and Receiving Cookies in PHP Scripts

Controlling HTTP Response Header Lines in PHP Scripts

MySQL Server Connection and Access Functions

Diffraction of light meaning. Functions to Manage Directories, Files and Images

SOAP Extension Function and Calling Web Services

Function

SOAP Server Functions and Examples

Localization Overview of Web Applications

Using Non-ASCII Characters in HTML Documents

Using Non-ASCII Characters as PHP Script String Literals

Receiving Non-ASCII Characters from Input Forms

'mbstring' Extension and Non-ASCII Encoding Management

'mbstring' - Multi-Byte String Extension

mb_convert_encoding() and Other mbstring Functions

Examples of Using 'mbstring' Functions

Managing HTTP Input and Output Encoding

Managing Non-ASCII Character Strings with MySQL Servers

Mb_convert_encoding Not Working

Configuring and Sending out Emails

Mb_convert_encoding Decode In Php

Outdated Tutorials

References

Text Encoding Converter

Full Version in PDF/EPUB