Home  >  Article  >  Backend Development  >  How to convert string to utf8 in php

How to convert string to utf8 in php

藏色散人
藏色散人Original
2020-10-14 16:43:592852browse

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)

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);//也可以这么用
  ?>

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!

Statement:
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