每次使用PHP和HTML时,口音都会以不同的方式打印 [英] Accents printed differently every time, in PHP and HTML

查看:100
本文介绍了每次使用PHP和HTML时,口音都会以不同的方式打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 acatl  nde 

ju rez

再次刷新页面几次后,它会像这样打印它们:

 acatlándejuárez

这个过程来回地重复。这不是应该在我的应用程序中发生的事情,因为我用这个词比较了很多事情。



我的头上已经有了utf-8

 < meta charset =UTF-8> 

使用的数据库排序规则:utf8_unicode_ci



导致这个错误的变量是这样分配的:

  $ theString =acatlan de juarez; 

$ a = array(
'animal'=> array('acatlándejuárez','hów','áre'),
'color'=> array('yóu','good','thanks')
);
= array(
'animal'=> array('acatlan de juarez','how','are'),
'color'=> array('you ','好','谢谢')
);
$ theString = str_replace($ b ['animal'],$ a ['animal'],$ theString);
$ b $ // $ theString现在是$ theString =AcatlándeJuárez

所以 $ theString 变量,当回显或查询时,有时会打印acatl ndeju rez,而其他时间会打印acatlándejuárez。




编辑

开始收到关于数据库查询的大量答案,它实际上与数据库无关,从数据库正确地输入了重音。问题在于上面代码中介绍的变量$ theString,它不会像打印出来一样用重音符号打印。






编辑2



我不知道现在发生了什么,我很困惑,在这个例子中:

  $ a = array(
'animal'=> array('acatlándejuárez','hów','áre'),
'color'=>数组('yóu','good','thanks')
);
echo $ a ['animal'] [0];

我的代码完美地输出了单词acatlándejuárez。

但是我有我的阵列$城市(完全像我的阵列$ a),但有州和城市。像这样:

  $ cities = array(
'state1'=> array('acatlándejuárez', 'city1','city2'),
'state2'=>数组('city1','city2','city3')
);
echo $ cities ['state1'] [0];

现在acatlándejuárez变成了acatl ndeju rez。为什么?!?






编辑3



问题解决了,它是另外一个函数,它检查字符集格式,谢谢大家。

和你几周前一样的问题,在修改数据库COLLATE和CHARSET以及我如何处理MySQL查询之后解决了问题。 我的文章在这里。


  1. 将您的数据库的默认集合设置为 utf8mb4 - utf8mb4_unicode_ci

  2. 确保表格字段的COLLATE属性也设置为 utf8mb4_unicode_ci 在您的HTML中添加< meta charset =utf-8> 标记

  3. 当您执行SELECT,INSERT,UPDATE在MySQL上调用 $ mysqli-> set_charset('utf8mb4');



<注意:我建议你阅读一般为什么 utf8mb4 或为什么不是 utf8 。另请阅读




$ b

  CREATE TABLE`quote`(
`id` int (10)unsigned NOT NULL AUTO_INCREMENT,
`code` char(4)COLLATE utf8mb4_unicode_ci NOT NULL',
`quote` varchar(255)COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY(`id `)
)ENGINE = MyISAM DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

示例选择

注意:注意 set_charset()

  $ mysqli - > set_charset( 'utf8mb4'); 
$ result = $ mysqli-> query('SELECT * FROM quote');
while($ row = $ result-> fetch_array(MYSQLI_ASSOC)){
//用$ row $ b $做一些事情}
$ b

示例插入/更新

  $ mysqli-> set_charset( 'utf8mb4'); 
$ sql =INSERT INTO quote(code,quote)VALUES(?,?);
// $ sql =UPDATE ......;
$ stmt = $ mysqli-> prepare($ sql);
$ stmt-> bind_param('ss',$ _POST ['code'],$ _POST ['quote']);
if(!$ stmt-> execute()){
echo $ stmt-> error;
}
echo'Nice one';


I'm echoing into my page some accented words, that sometimes are printed like this:

acatl��n de ju��rez

And after refreshing the page a few times again, it prints them like this:

acatlán de juárez

And the process repeats back and forth. This is not something that should be happening in my application because I'm comparing many things with this words.

I already have utf-8 on my header

<meta charset="UTF-8">

The database collation used: utf8_unicode_ci

The variable causing this errors is assigned like this:

$theString = "acatlan de juarez";

$a = array(
    'animal' => array('acatlán de juárez','hów','áre'),
    'color'  => array('yóu','good','thanks')
);
$b = array(
    'animal' => array('acatlan de juarez','how','are'),
    'color'  => array('you','good','thanks')
);
$theString = str_replace($b['animal'], $a['animal'], $theString);

// $theString now is $theString = "Acatlán de Juárez"

So $theString variable, when echoed or doing a query, sometimes prints "acatl��n de ju��rez" and some other times prints "acatlán de juárez".


EDIT

I'm starting to receive alot of answers regarding database queries, it actually has nothing to do with the Database, accents are correctly ouputed from the database. Problem is with the variable $theString explained in the code above, it is not printed like it should, with accents.


EDIT 2

I don't know what's happening now, I'm really confused, in this example:

$a = array(
'animal' => array('acatlán de juárez','hów','áre'),
'color'  => array('yóu','good','thanks')
);
echo $a['animal'][0];

My code outputs the word "acatlán de juárez" perfectly.

But the I have my array $cities (exactly like my array $a) but with states and cities. Like this:

$cities = array(
'state1' => array('acatlán de juárez','city1','city2'),
'state2'  => array('city1','city2','city3')
);
echo $cities['state1'][0];

And now "acatlán de juárez" becomes "acatl��n de ju��rez".... WHY?!?


EDIT 3

Question solved, it was another function bugging the charset format, thank you everyone.

解决方案

I had exactly the same issue as yours a few weeks ago and the solved the problem after modifying database COLLATE and CHARSET as well as how I handle MySQL queries. My post is here.

  1. Set you DB's "default collection" to utf8mb4 - utf8mb4_unicode_ci
  2. Make sure "COLLATE" property of table fields are set to utf8mb4_unicode_ci as well.
  3. Set "meta" tag to <meta charset="utf-8"> in your HTML.
  4. When you do SELECT, INSERT, UPDATE on MySQL, call $mysqli->set_charset('utf8mb4');

Note: I suggest you to read up on why utf8mb4 or why not utf8 in general. Also read this.

EXAMPLE TABLE

CREATE TABLE `quote` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `code` char(4) COLLATE utf8mb4_unicode_ci NOT NULL',
  `quote` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

EXAMPLE SELECT

Note: Pay attention to set_charset()

$mysqli->set_charset('utf8mb4');
$result = $mysqli->query('SELECT * FROM quote');
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
   //Do something with $row
}

EXAMPLE INSERT/UPDATE

$mysqli->set_charset('utf8mb4');
$sql = "INSERT INTO quote (code, quote) VALUES (?, ?)";
//$sql = "UPDATE ......";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('ss', $_POST['code'], $_POST['quote']);
if (! $stmt->execute()) {
    echo $stmt->error;
}
echo 'Nice one';

这篇关于每次使用PHP和HTML时,口音都会以不同的方式打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆