Home Backend Development PHP Problem How to change string to UTF8 in PHP?

How to change string to UTF8 in PHP?

Jun 15, 2020 pm 04:32 PM
php basics

How to change string to UTF8 in PHP?

How to change a string to UTF8 in PHP?

In PHP, you can use the "iconv()" function to change the string to UTF8. The function of this function is to convert the string according to the required character encoding. Its syntax is "iconv(in, out,str)", when using it, set in to the encoding of the string, set out to UTF8, and set str to the string to be converted.

Conversion example

$str = "123456789";

$str = iconv('ASCII', 'UTF8', $str);
Copy after login

Usage example

<?php
//some German
$utf8_sentence = &#39;Weiß, Goldmann, Göbel, Weiss, Göthe, Goethe und Götz&#39;;

//UK
setlocale(LC_ALL, &#39;en_GB&#39;);

//transliterate
$trans_sentence = iconv(&#39;UTF-8&#39;, &#39;ASCII//TRANSLIT&#39;, $utf8_sentence);

//gives [Weiss, Goldmann, Gobel, Weiss, Gothe, Goethe und Gotz]
//which is our original string flattened into 7-bit ASCII as
//an English speaker would do it (ie. simply remove the umlauts)
echo $trans_sentence . PHP_EOL;

//Germany
setlocale(LC_ALL, &#39;de_DE&#39;);

$trans_sentence = iconv(&#39;UTF-8&#39;, &#39;ASCII//TRANSLIT&#39;, $utf8_sentence);

//gives [Weiss, Goldmann, Goebel, Weiss, Goethe, Goethe und Goetz]
//which is exactly how a German would transliterate those
//umlauted characters if forced to use 7-bit ASCII!
//(because really ä = ae, ö = oe and ü = ue)
echo $trans_sentence . PHP_EOL;

?>
Copy after login
<?php
$tab = array("UTF-8", "ASCII", "Windows-1252", "ISO-8859-15", "ISO-8859-1", "ISO-8859-6", "CP1256");
$chain = "";
foreach ($tab as $i)
    {
        foreach ($tab as $j)
        {
            $chain .= " $i$j ".iconv($i, $j, "$my_string");
        }
    }

echo $chain;
?>
Copy after login

Recommended tutorial: "PHP Tutorial"

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

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What front-end developers need to know about PHP What front-end developers need to know about PHP Mar 29, 2024 pm 03:09 PM

As a front-end developer, understanding PHP is very necessary. Although PHP is a back-end development language, mastering a certain amount of PHP knowledge can help front-end developers better understand the entire web development process, improve work efficiency, and collaborate better with back-end developers. In this article, we will discuss some PHP-related knowledge that front-end developers need to know and provide specific code examples. What is PHP? PHP (HypertextPreprocessor) is a server

A Beginner's Guide to PHP A Beginner's Guide to PHP May 25, 2023 am 08:03 AM

PHP is a popular front-end programming language. It is powerful, easy to learn and use, and is widely used in website development and maintenance. For beginners, getting started with PHP requires a certain amount of learning and mastering. Here are some guides for beginners in PHP. 1. Learn basic concepts Before learning PHP, you need to understand some basic concepts. PHP is a scripting language that issues instructions to web servers. Simply put, you can use PHP to generate HTML code and send it to the browser to eventually render on the web page

Weak foundation in PHP written test? Try these study methods! Weak foundation in PHP written test? Try these study methods! Mar 01, 2024 pm 01:39 PM

PHP, as a scripting language widely used in web development, has become one of the necessary skills for many Internet companies to recruit technical talents. However, for some learners who are just getting started or have a weak foundation, learning PHP may encounter some difficulties. How can you better improve your basic skills when facing the PHP written test? Next, we will introduce some learning methods, hoping to help everyone improve the basic knowledge and skills of PHP. 1. Develop the habit of reading PHP official documentation. PHP official documentation is a learning

Essential for beginners: PHP basic errors and solutions Essential for beginners: PHP basic errors and solutions May 11, 2023 am 08:28 AM

In the process of learning PHP, beginners often encounter various errors. Although this is a natural process of learning, many beginners often lose patience because of improper handling of mistakes. This article will introduce basic PHP errors and solutions, aiming to help beginners get started with PHP more easily. 1. Syntax error 1.1 Missing semicolon In PHP, statements must end with a semicolon. If you accidentally omit a semicolon, an error will be reported. For example, the following code results in an error: &lt;?phpecho"He

PHP Values ​​Overview: Understand the important concepts of PHP PHP Values ​​Overview: Understand the important concepts of PHP Mar 22, 2024 pm 03:09 PM

Overview of PHP values: To understand the important concepts of PHP, specific code examples are required. PHP (Hypertext Preprocessor) is a scripting language widely used in web development. It can be embedded in HTML or executed as a standalone script. In web development, it is important to understand some important concepts of PHP to write efficient and maintainable code. In this article, we will introduce some important concepts of PHP and provide specific code examples to help readers understand better. variable changes

PHP basic tutorial array function PHP basic tutorial array function Jun 20, 2023 pm 01:39 PM

Array function is one of the most commonly used functions in PHP, which can be used to create, operate and manage arrays. When developing applications, using array functions can greatly improve development efficiency. This article will introduce some basic usage and examples of array functions in PHP to help everyone better understand and master array functions. 1. Creation and initialization of arrays Arrays in PHP can be created in the following ways: //Create arrays through the array() function $arr=array("a",&q

10 Tips for Reading PHP Documentation 10 Tips for Reading PHP Documentation May 24, 2023 pm 09:21 PM

PHP is a very popular open source server-side scripting language that is widely used in web development. To become a good PHP programmer, reading official documentation is essential. Whether you are a beginner or an experienced developer, these tips will help you read PHP documentation more effectively. Understand the document structure PHP official documentation is divided into multiple parts, including manuals, reference manuals, FAQs, extension library documents, etc. Before you start reading, understand the structure of the document and find the parts you need. Using the search function PHP documentation

Necessary for building a dreamweaver website: Master several key knowledge points of PHP Necessary for building a dreamweaver website: Master several key knowledge points of PHP Mar 27, 2024 pm 03:09 PM

In today's Internet era of information explosion, websites have become an important way for display and promotion in all walks of life, and PHP, as the most popular server-side scripting language, is undoubtedly one of the essential skills for many website developers. To become proficient in PHP, you first need to master several key knowledge points and deepen your understanding through specific code examples. 1. PHP basic syntax The basic syntax of PHP is similar to most programming languages, including variables, data types, operators, conditional statements, loop statements, etc. Here is a simple example

See all articles