Home > Backend Development > PHP Problem > How to convert encoding in php string

How to convert encoding in php string

藏色散人
Release: 2023-03-08 12:12:01
Original
3403 people have browsed it

php string conversion encoding method: 1. The general iconv function performs string conversion encoding; 2. Use the "mb_convert_encoding" function to handle multi-byte encoding conversion.

How to convert encoding in php string

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer.

php String encoding conversion

php provides two string conversion encoding functions, one is a general iconv function, and the other is a multi-byte encoding conversion function mb_convert_encoding, you need to enable the extension php_mbstring

string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )
Copy after login

Use:

It is found that iconv will make an error when converting the character "-" to gb2312. If there is no ignore parameter, all strings following this character cannot be save. No matter what, this "-" cannot be converted successfully and cannot be output. In addition, mb_convert_encoding does not have this bug.

mb_convert_encoding can specify multiple input encodings, which will be automatically recognized based on the content, but the execution efficiency is much worse than iconv;

如:s t r = m b c o n v e r t e n c o d i n g ( str = mb_convert_encoding(str=mb convert encoding(str,“euc-jp”,“ASCII,JIS,EUC-JP,SJIS,UTF- 8”);“ASCII,JIS,EUC-JP,SJIS,UTF-8”的顺序不同效果也有 差异
Copy after login

Generally use iconv, only Use the mb_convert_encoding function when you are unable to determine what the original encoding is, or the iconv conversion cannot be displayed normally.

Example:

$content = iconv(“GBK”, “UTF-8”, c o n t e n t ) ; ‘ ‘ content); ` `content);‘‘content = mb_convert_encoding($content, “UTF-8”, “GBK”);`
Copy after login

[Recommended learning: PHP video tutorial

The above is the detailed content of How to convert encoding in php string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template