PHP and MySQL relay UTF8 coding problem Summary _php tips

Source: Internet
Author: User
Tags php and mysql truncated

If you need to internationalize a website, you need to convert the code from GB2312 to UTF-8, which has a lot of problems to note, if not converted thoroughly, there will be a lot of coding problems appear!
PHP page UTF-8 encoding problem
1. In the code begins to add a line: Header ("Content-type:text/html;charset=utf-8");
2.PHP file Encoding Problem click the Editor's menu: "File"-> "Save As", you can see the current file encoding to ensure that the file encoding is: UTF-8, if it is ANSI, you need to change the code to: UTF-8.
3.PHP file Header BOM problem: PHP file must not have BOM label, otherwise, will appear session can not use the situation, and have a similar hint: Warning:session_start () [Function.session-start]: Cannot send session cache Limiter-headers already sent this is because in the execution of Session_Start (), the entire page can not have output, but when the previous PHP page exists BOM label, PHP is the BOM label as output, so it went wrong! So the PHP page must delete the BOM label
How to delete this BOM label:
1. You can use Dreamweaver to open the file and save it again, that is, you can remove the BOM label!
2. You can open the file with EditPlus,
And in the menu "preferences"-> "file"-> "UTF-8 identity," set to: "Always delete the signature", and then save the file, that is, you can remove the BOM label!
3.PHP when saving files as an attachment, the UTF-8 encoding problem:PHP as an attachment to save the file, file name must be GB2312 code, otherwise, if the file name in Chinese, will be displayed garbled: If your PHP itself is UTF-8 encoded file, you need to change the file name variable from UTF-8 to Gb2312:iconv ("UTF-8", "GB2312", "$filename");
4. When the title of the article is truncated, there is garbled or "? "Question mark:
General article title for a long time, will show a part of the title, will be truncated to the title of the article, because a UTF-8 encoded format of the Chinese characters will occupy 3 character width, the interception of the title, sometimes only intercepted to a Chinese character of 1 characters or 2 character width, did not intercept the complete, will appear garbled or "? Question mark, use the following function to intercept the title, there is no problem:

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; 
}

The problem of using UTF-8 encoding in MySQL database
1. When creating databases and datasheets with phpMyAdmin, set the collation to: "Utf8_general_ci" or execute the statement:

CREATE DATABASE ' dbname ' DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;

When you create a datasheet: If the field is in Chinese, you need to set the collation to: "Utf8_general_ci", if the field is stored in English or numbers, the default is OK.
The corresponding SQL statement, for example:

CREATE TABLE ' test ' ( 
' id ' INT NOT NULL, 
' name ' VARCHAR "CHARACTER SET UTF8 COLLATE utf8_general_ci NOT null , 
PRIMARY KEY (' id ') 
) ENGINE = MYISAM;

2. Read and write database in PHP
After connecting to the database:

$connection = mysql_connect ($host _name, $host _user, $host _pass);

Add two lines:

mysql_query ("Set character set ' UTF8 '");/Read library 
mysql_query ("Set names ' UTF8 '");//write Library

I hope this article will help you with the Php+mysql program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.