一个网站如果需要国际化,就需要将编码从GB2312转成UTF-8,其中有很多的问题需要注意,如果没有转换彻底,将会有很多的编码问题出现!
PHP页面转UTF-8编码问题
1.在代码开始出加入一行: header("Content-Type: text/html;charset=utf-8");
2.PHP文件编码问题 点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8,如果是ANSI,需要将编码改成:UTF-8。
3.PHP文件头BOM问题: PHP文件一定不可以有BOM标签,否则,会出现session不能使用的情况,并有类似的提示: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent 这是因为,在执行session_start() 的时候,整个页面不能有输出,但是当由于前PHP页面存在BOM标签,PHP把这个BOM标签当成是输出了,所以就出错了! 所以PHP页面一定要删除BOM标签
删除这个BOM标签的方法:
1.可以用Dreamweaver打开文件,并重新保存,即可以去除BOM标签!
2.可以用EditPlus打开文件,并在菜单“首选项”->“文件”->"UTF-8标识",设置为:“总是删除签名”,然后保存文件,即可以去除BOM标签!
3.PHP以附件形式保存文件的时候,UTF-8编码问题: PHP以附件形式保存文件,文件名必须是GB2312编码,否则,如果文件名中有中文的话,将是显示乱码: 如果你的PHP本身是UTF-8编码格式的文件,需要将文件名变量由UTF-8转成GB2312: iconv("UTF-8", "GB2312", "$filename");
4.截断显示文章标题时,出现乱码或者“?”问号的问题:
一般文章标题很长的时候,会显示一部分标题,会对文章标题进行截断,由于一个UTF-8编码格式的中文字符会占用3个字符宽度,截取标题的时候,有时会只截取到一个中文字符的1个字符或2字符宽度,没截取完整,将出现乱码或“?”问号的情况,用下面的函数截取标题,就不会有问题:
function get_brief_str($str, $max_length) { echo strlen($str) . ""; if (strlen($str) > $max_length) { $check_num = 0; for ($i = 0; $i < $max_length; $i++) { if (ord($str[$i]) > 128) $check_num++; } if ($check_num % 3 == 0) $str = substr($str, 0, $max_length) . "..."; else if ($check_num % 3 == 1) $str = substr($str, 0, $max_length +2) . "..."; else if ($check_num % 3 == 2) $str = substr($str, 0, $max_length +1) . "..."; } return $str; }
MYSQL数据库使用UTF-8编码的问题
1.用phpmyadmin创建数据库和数据表 创建数据库的时候,请将“整理”设置为:“utf8_general_ci”或执行语句:
CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
创建数据表的时候:如果是该字段是存放中文的话,则需要将“整理”设置为:“utf8_general_ci”,如果该字段是存放英文或数字的话,默认就可以了。
相应的SQL语句,例如:
CREATE TABLE `test` ( `id` INT NOT NULL , `name` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM ;
2.用PHP读写数据库
在连接数据库之后:
$connection = mysql_connect($host_name, $host_user, $host_pass);
加入两行:
mysql_query("set character set 'utf8'");//读库 mysql_query("set names 'utf8'");//写库
希望本文所述对大家的PHP+MySQL程序设计有所帮助。

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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),

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.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment