How to convert string to utf8 in php

藏色散人
Release: 2023-03-06 16:58:01
Original
2818 people have browsed it

In PHP, you can use the iconv function to convert a string into utf8 encoding. The syntax is "iconv('format to be converted', 'converted format', 'converted data');" .

How to convert string to utf8 in php

Recommended: "PHP Video Tutorial"

PHP Convert the string to the character set format UTF8/GB2312/ GBK function iconv()

iconv() introduction

iconv function can convert a known character set file into another known character set file

iconv('Format to be converted', 'Converted format', 'Converted data');

However, conversion often causes errors. Generally, you need to add "//IGNORE" after the converted encoding:

ignore means to ignore errors during conversion. Without the ignore parameter, all strings following the character cannot be saved.

iconv("UTF-8", "GB2312//IGNORE", $data)
Copy after login

Example:

<?php
  header("content-type:text/html;charset=utf8");
      echo $str = "你好,你是卖咖啡的嘛?";

      echo "<br>";
      echo $gb = iconv(&#39;UTF-8&#39;,&#39;GB2312&#39;,$str);
      echo "<br>";
      echo $utf = iconv(&#39;GB2312&#39;,&#39;utf-8&#39;,$gb);
      echo "<br>";
   echo $gb = iconv(&#39;GB2312&#39;,&#39;utf-8&#39;,$gb);//也可以这么用
  ?>
Copy after login

mb_detect_encoding( $content, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));

Can determine what encoding format it is

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

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!