search
Homephp教程php手册PHP教程:把字符串转换成UTF-8编码

PHP教程:把字符串转换成UTF-8编码

Jun 06, 2016 pm 08:10 PM
phputf-8deal withstringReplace withTutorialcoding

PHP处理数据,肯定离不开对字符串的处理,而我们又必须要让数据库编码与网页或者说应用的编码一致,否则就会出现乱码。所以呢,在进行数据存储或者更新之前——调用数据存储过程时,我们必须对数据进行编码上的统一。而很多的网页都是基于UTF-8编码的,我们

PHP处理数据,肯定离不开对字符串的处理,而我们又必须要让数据库编码与网页或者说应用的编码一致,否则就会出现乱码。所以呢,在进行数据存储或者更新之前——调用数据存储过程时,我们必须对数据进行编码上的统一。而很多的网页都是基于UTF-8编码的,我们现在就来看看,在进行数据存储之前,我们应该怎么样做到编码的转换。在这里,我们就以转换字符串编码成UTF-8为例,看看应该怎么处理。

函数

<?php ? ??
function?fixEncoding($in_str){? ??
??//检测编码 ??
??cur_encoding?=?mb_detect_encoding($in_str)?;? ??
??if($cur_encoding?==?"UTF-8"?&&?mb_check_encoding($in_str,"UTF-8"))? ??
????return?$in_str;? ??
??else? ??
????return?utf8_encode($in_str);? ??
}? ??
?>??

这里呢,用到两个比较重要的函数,一个是获取编码的mb_detect_encoding函数,《PHP教程:利用mb_detect_encoding获取字符串的编码》;利用mb_check_encoding函数检查编码。值得我们注意的是,在进行数据存储或者更新的时侯,我们必须使用addslashes()函数对进行处理输出后的结果!!

声明: 本文采用 BY-NC-SA 协议进行授权 | IT路人
转载请注明转自《PHP教程:把字符串转换成UTF-8编码》

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment