---
title: "PHP用chr()生成的string与UTF-8问题"
author: "kooyee"
date: "2010-03-10T17:08:18Z"
tags: []
categories: ["编程语言"]
slug: "php-chr-string-utf-8"
draft: false
lastmod: "2010-03-10T17:08:18Z"
id: 612236
url: "https://kooyee.iteye.com/blog/612236"
---

PHP用chr()生成的string在保存到utf8的数据库中出错编码问题。

 

需要**utf8\_encode** ()来转换string的编码。

utf8\_encode — Encodes an ISO-8859-1 string to UTF-8

 

例子：

```
utf8_encode(chr($x));

for ($i = 0; $i < 20; $i++)
        $str .= chr(mt_rand(0, 255));
utf8_encode($str);
```