简体   繁体   English

PHP字符问题

[英]Php problems with characters

I am building an app with Apache cordova for the support team for my company and everything was ok when I was using a test database in UTF8 was working. 我正在使用Apache cordova为我公司的支持团队构建一个应用程序,当我在UTF8中使用测试数据库时,一切正常。

Then when I was implement the real db I notice it was encoded with win-1252. 然后,当我实现真正的数据库时,我注意到它是用win-1252编码的。

The problem is, even the db is with win-1252 we have many rows using special caracters like "ç" and "~" and "´" and "`" and with that when I am running the php all rows in the tables in my db will not show becasue of that. 问题是,即使数据库与win-1252一起使用,我们也有许多行使用特殊字符,例如“ç”和“〜”和“´”和“`”,并且当我运行php中的表时,我的数据库不会显示出来的。

Keep in mind I cann't convert the db to utf8. 请记住,我无法将数据库转换为utf8。

ps:The solution I see is go to each row and remove that caracters but isn't a good solution(about 20,000 rows) ps:我看到的解决方案是排到每一行并删除那些角色,但这不是一个好的解决方案(大约20,000行)

........................ ...........................

PHP file: PHP文件:

header("Access-Control-Allow-Origin: *");

$dbconn = pg_connect("host=localhost dbname=bdgestclientes2 
    user=postgres password=postgres") 
    or die('Could not connect: ' . pg_last_error());

$data=array();

$q=pg_query($dbconn,"SELECT * FROM clientes WHERE idcliente = 3");

$row=pg_fetch_object($q)){$data[]=$row}; $ row = pg_fetch_object($ q)){$ data [] = $ row};

echo json_encode($data); 回声json_encode($ data);

我只需要在php中添加一行即可编码为unicode,以便可以使用数据并显示其方式

pg_set_client_encoding($dbconn, "UNICODE");

That shouldn't be a problem at all. 这根本不是问题。

Windows-1252 supports “ç” (code point 0xE7), “~” (code point 0x7E), “`” (code point 0x60) and “´” (code point 0xB4). Windows-1252支持“ç”(代码点0xE7),“〜”(代码点0x7E),“`”(代码点0x60)和“´”(代码点0xB4)。

PostgreSQL will automatically convert the characters to the database encoding. PostgreSQL会自动将字符转换为数据库编码。

You will get problems if you want to store characters that do not occur in Windows-1252, like “Σ”. 如果要存储Windows-1252中不会出现的字符(例如“Σ”),则会遇到问题。 In that case, the correct solution is to use a database with a different encoding (UTF8). 在这种情况下,正确的解决方案是使用具有不同编码(UTF8)的数据库。

If you cannot do that, you'll have to store the strings as binary objects (data type bytea ) and handle encoding in your application. 如果不能这样做,则必须将字符串存储为二进制对象(数据类型bytea ),并在应用程序中处理编码。 That will only work well if you don't need to process these functions in the database (eg, use an index for case insensitive search). 仅当您不需要在数据库中处理这些功能时(例如,使用索引进行不区分大小写的搜索),该方法才能很好地工作。

I have a similar issue, where I cannot modify the database setup, but I use php's html entity encode to work around: 我有一个类似的问题,我无法修改数据库设置,但是我使用php的html实体编码来解决:

I removed the html key elements from the native htmlentities because I work with wysiwyg editors and need to keep the content like that. 我从本地htmlentities中删除了html关键元素,因为我与所见即所得的编辑器一起工作,并且需要保持这样的内容。 If you have no such limitations you can just use htmlentities on the string. 如果没有这种限制,则可以在字符串上使用htmlentities。

function makeFriendly($string)
        $list = get_html_translation_table(HTML_ENTITIES);
        unset($list['"']);
        unset($list['\'']);
        unset($list['<']);
        unset($list['>']);
        unset($list['&']);
        $search = array_keys($list);
        $replace = array_values($list);
        $search = array_map('utf8_encode', $search);
        str_replace($replace, $search, $string);
}

If I need the actual characters I can always call html_entity_decode on the database string to get the 'real' string. 如果我需要实际的字符,我总是可以在数据库字符串上调用html_entity_decode以获得“真实”字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM