非拉丁字符&哎哟 [英] Non Latin Characters & ouch

查看:218
本文介绍了非拉丁字符&哎哟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要了解Cake PHP,它发现了关于PHP / MySQL字符集的最佳实践的一般性问题,我希望这里可以回答。



我的(实践)系统包含一个电影的mysql表。此列表源自Excel工作表,导出为CSV,并通过phpMyAdmin导入。



我注意到,具有更多外来字形的标题在浏览器中呈现问题,例如Amélie中的é。除非通过 htmlentities 转换为,否则使用Cake或纯PHP来渲染 & eacute;



如果我使用Cake输入表单输入< alt> 0233 ,这在源代码中正确呈现,但是& Atilde;& copy; 通过 htmlentities



经过一个快速搜索,我决定也许UTF-8会修复的东西,因此我




  • 更改了PHP源代码,将CSV文件编码更改为UTF-8

  • 确保< meta>

  • 确保我的浏览器认为文档是UTF-8(他们这样做)

  • 将MySQL数据库上的排序规则更改为utf-8 general_ci(作为可靠的UTF-8选项的教育样本)

  • 删除并重新导入我的数据



但是,我还是坚持。我注意到,phpMyAdmin设法在浏览记录时在其HTML源中正确地呈现字符。



然而,我想知道是否有人可以提供最好的答案:




  • 将我的数据从Excel移到MySQL以保留字形的最好方法是什么?

  • 为了适应这种情况,我的表格的最佳设置是什么?
  • 我喜欢使用UTF-8本地显示é,我可以在Cake做什么,以避免负载调用htmlentities的喜欢,即有一个配置设置或方式我设置让这个更友好,让 Html->链接工作吗?



有些代码,以防万一:



电影控制器摘录。

  function index(){
$ this-> set('movies',$ this-> Movie-> find('all'));

}

index.ctp视图摘录

 <?php foreach($ movies as $ movie):?> 
< tr>
< td><?php echo $ movie ['Movie'] ['id']; ?>< / td>
< td><?php echo htmlentities($ movie ['Movie'] ['title']); ?>
< td><?php echo $ this-> Html-> link($ movie ['Movie'] ['title'],
array('controller'=& movies','action'=>'view',$ movie ['Movie'] ['id'])); ?>
< / td>

< td><?php echo $ this-> Html-> link(Edit,
array('action'=>'edit',$ movie ['Movie'] ['id'])); ?>
< / td>

< td>
<?php echo $ this-> Html-> link('Delete',array('action'=>'delete',$ movie ['Movie'] ['id' null,'你确定吗?')?>
< / td>

< / tr>
<?php endforeach; ?>感谢您提供任何帮助/提示。

解决方案

在导入数据时,请确保MySQL连接设置为UTF-8。



您可以使用 SET NAMES'utf - 设置连接的字符集, 8'; 在您的SQL文件的开头。


I'm getting to know Cake PHP, which has unearthed a general question about best practice in terms of PHP / MySQL character set stuff, which I'm hoping can be answered here.

My (practice) system contains a mysql table of movies. This list was sourced from an Excel sheet, which was exported as CSV, and imported via phpMyAdmin.

I noticed that titles with more "exotic" glyphs have issues rendering in the browser, eg The é in Amélie. Using Cake or plain PHP, it renders as a ?, unless transformed via htmlentities into a &eacute;. Links with the special characters don't render at all.

If I use my Cake input form to enter an <alt>0233, this is rendered correctly in source, but as &Atilde;&copy; via htmlentities.

After a quick SO search, I decided maybe UTF-8 would fix stuff, hence I

  • changed the PHP source, and CSV file encoding to UTF-8
  • made sure the <meta> stuff was there (it was already via Cake's default layout).
  • made sure my browsers thinks the doc is UTF-8 (they do)
  • changed the collation on the MySQL DB to utf-8 general_ci (as an educated stab from avalable UTF-8 options)
  • deleted and reimported my data

However, I'm still stuck. I note that phpMyAdmin manages to render the characters "correctly" in it's HTML source when browsing records.

I sense that document encoding's to blame, however, am wondering if someone can provide the best answer to:

  • what's the best way to move my data from Excel to MySQL to preserve glyphs?
  • what's the optimum settings for my tables to accommodate this?
  • I'd prefer to use UTF-8 to natively display the likes of é, what can I do in Cake to avoid making loads of calls to the likes of htmlentities ie is there a configuration setting or way I set stuff up that makes this more friendly and lets Cake native helpers like Html->link work?

Some code, just in case:

movies controller excerpt..

function index() {
        $this->set('movies' , $this->Movie->find('all'));

}

index.ctp view excerpt

<?php foreach ($movies as $movie): ?>
<tr>
    <td><?php echo $movie['Movie']['id']; ?></td>
    <td><?php echo htmlentities($movie['Movie']['title']); ?>
    <td><?php echo $this->Html->link($movie['Movie']['title'] , 
    array('controller' => 'movies' , 'action' => 'view' , $movie['Movie']['id'])); ?>
    </td>

    <td><?php echo $this->Html->link("Edit", 
    array('action' => 'edit' , $movie['Movie']['id'])); ?>
    </td>

    <td>
    <?php echo $this->Html->link('Delete', array('action' => 'delete', $movie['Movie']['id']), null, 'Are you sure?')?>
    </td>

</tr>
<?php endforeach; ?>

Thanks in advance for any help / tips.

解决方案

Make sure the MySQL connection is set to UTF-8 while importing the data. The collation is only used for sorting and comparison, not for saving data.

You can set the charset of the connection using SET NAMES 'utf-8'; in the beginning of your SQL file.

这篇关于非拉丁字符&amp;哎哟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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