Home > Backend Development > PHP Problem > How to convert characters in php

How to convert characters in php

hzc
Release: 2023-03-01 13:58:02
Original
3296 people have browsed it

How to convert characters in php

How to convert characters to encoding in php:

The first one, iconv() function:

//iconv('目标编码', '原编码', '字符串')
$encode = mb_detect_encoding( $str, array('ASCII','UTF-8','GB2312','GBK'));
if ( !$encode =='UTF-8' ){
     $str = iconv('UTF-8',$encode,$str);
}
Copy after login

The second one The third type, mb_convert_encoding()

//mb_convert_encoding('要转编码的字符串','目标编码','原编码')
//mb_detect_encoding('字符串'):
mb_convert_encoding($str,'utf-8',mb_detect_encoding($str))
Copy after login

The third type, convert to base64 string

base64_encode($canonical_request)
Copy after login

The fourth type, convert URL encoding

// urlencode()函数原理就是首先把中文字符转换为十六进制,然后在每个字符前面加一个标识符%,对字符串中除了 -_. 之外的
所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+)。
urlencode('URL字符串')

// urldecode()函数与urlencode()函数原理相反,用于解码已编码的 URL 字符串,其原理就是把十六进制字符串转换为中文字符。

urldecode('URL字符串')
Copy after login

Recommended tutorial: "php Tutorial

The above is the detailed content of How to convert characters in php. 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