PHP转换外国字符与口音 [英] PHP convert foreign characters with accents

查看:145
本文介绍了PHP转换外国字符与口音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我试图将一些文本与数据库中的文本进行比较。在数据库中,任何具有重音的文本都会像html(即& eacute;)中编码,当我将数据库文本与我的字符串它不匹配,因为我的字符串只是显示é..当我使用php函数htmlentities编码字符串首先é变成é奇怪的?使用htmlspecialchars不会编码é所有..

Hi I'm trying to compare some text to the text in a database.. in the database any text with an accent is encoded like in html (ie. é) when I compare the database text to my string it doesn't match because my string just shows é .. when I use the php function htmlentities to encode the string first the é turns into é weird? using htmlspecialchars doesn't encode the é at all..

你会建议我比较é和é以及所有其他重音字符?

how would you suggest I compare é to é as well as all the other accented characters?

推荐答案

您需要发送正确的字符集到htmlentities。看起来您使用的是UTF-8,但默认值为ISO-8859-1。像这样更改:

You need to send in the correct charset to htmlentities. It looks like you're using UTF-8, but the default is ISO-8859-1. Change it like this:

$encoded = htmlentities($text, ENT_COMPAT, 'UTF-8');

另一个解决方案是在编码之前将文本转换为ISO-8859-1,但这可能会破坏信息(ISO-8859-1不包含与UTF-8几乎相同的字符)。如果你想尝试,请这样做:

Another solution is to convert the text to ISO-8859-1 before encoding, but that may destroy information (ISO-8859-1 does not contain nearly as many characters as UTF-8). If you want to try that instead, do like this:

$encoded = htmlentities(utf8_decode($text));

这篇关于PHP转换外国字符与口音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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