php with special characters like ñ

2020-05-28 10:29发布

At first I thought the problem was when I return echo json_encode($row) from an ajax call that results with ñ are changed to NULL. But after testing I found out that the problem exists way before that.

In a sample php file with:

$test = "Nuñez"
echo $test

the result is just Nu�ez

I've searched around but none of the suggested solutions work. Like:

mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
mb_language('uni');
mb_regex_encoding('UTF-8');
ob_start('mb_output_handler');

or <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />, or header('content-type: text/html; charset: utf-8');. And some more solutions that I've already forgotten, believe me I tried a lot.

That's just the beginning of it, I'm hoping that won't be a problem with mysql since my database is in utf-8 so is my $mysqli charset. But I think I can't say the same for ajax json_encode. But nevermind, one problem at a time. Can anybody please help me. Thanks a lot!

PROBLEM SOLVED I just had to set "Encode in UTF-8" in Notepad++, as it was in "Encode in ANSI" before.

6条回答
我想做一个坏孩纸
2楼-- · 2020-05-28 10:49

Try this

Its works for me.

$test = "Nuñez";

echo html_entity_decode(htmlentities($test));
查看更多
Deceive 欺骗
3楼-- · 2020-05-28 10:54

Face the same issue with the string "CASTAÑO".

I've tried posted solutions.

I used the ini_set('default_charset', 'utf-8'); directive.

  • The selected record showed "CASTA�O"
  • Using utf8-decode showed "CASTA?O"
  • Using utf8_encode shows the proper string "CASTAÑO"
查看更多
别忘想泡老子
4楼-- · 2020-05-28 11:01

for me

$test = "Nuñez";
echo $test;

shows Nuñez

You may try

$test = "Nuñez";
echo utf8_decode($test);

or

$test = utf8_encode("Nuñez");
echo utf8_decode($test);
查看更多
家丑人穷心不美
5楼-- · 2020-05-28 11:01

when you connect to your mysql db, set charset to utf-8, like ->

$sql_con = mysql_connect($sql_host, $sql_user, $sql_pass);
mysql_query('SET NAMES utf8');
查看更多
不美不萌又怎样
6楼-- · 2020-05-28 11:04

consider setting default_charset, this worked for me

ini_set('default_charset', 'utf-8');

查看更多
乱世女痞
7楼-- · 2020-05-28 11:08

to show correctly latin characters like ñ Ñ á é í ó ú, etc.. in browsers, you have to use iso-8859-1 instead of UTF-8 encoding http://www.w3schools.com/tags/ref_entities.asp

best regards!

查看更多
登录 后发表回答