我有一个网络应用程序(使用ExtJS构建),我让用户更新他们的基本信息。我对大多数更新例程都没有问题。但是,当我尝试更新名称中包含anñ的用户时,PHP将其更改为Unicode u00f1。
例如,我传递了名称Añana,PHP显示的是Au00f1ana,其中"u00f1“替换了"ñ”。
我已经尝试将字符集设置为utf-8、htmlspecialchars、mb_convert_encoding、utf8-decode和html_entity_decode,但都不起作用。
我解决这个问题的方法是使用strpos和substr_replace将utf代码替换为原始字符。
if(strpos($my_string, 'u00f1') !== FALSE){
$start_index = strpos($my_string, "u00f1");
$last_name = substr_replace($my_string, "ñ", $start_index, 5);
}
elseif(strpos($my_string, 'u00F1') !== FALSE){
$start_index = strpos($my_string, "u00F1");
$last_name = substr_replace($my_string, "Ñ", $start_index, 5);
}对于更多上下文,这是我使用的存储: Ext.define('AppName.store.MyStore',{ extend:'Ext.data.Store',
requires: [
'AppName.model.model_for_store',
'Ext.data.proxy.Ajax',
'Ext.data.reader.Json',
'Ext.data.writer.Json'
],
constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
remoteFilter: true,
remoteSort: true,
storeId: 'MyStore',
batchUpdateMode: 'complete',
model: 'AppName.model.model_for_store',
proxy: {
type: 'ajax',
batchActions: false,
api: {
create: 'folder/folder/create.php',
read: 'folder/folder/read.php',
update: 'folder/folder/update.php',
destroy: 'folder/folder/delete.php'
},
url: '',
reader: {
type: 'json',
keepRawData: true,
messageProperty: 'message',
rootProperty: 'data'
},
writer: {
type: 'json',
writeAllFields: true,
encode: true,
rootProperty: 'data'
}
}
}, cfg)]);
}
});这是被触发进行更新的PHP文件的开始:
<?php
require_once('../db_init.php');
require_once '../lib/response.php';
require_once '../lib/request.php';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('default_charset', 'utf-8');
header("content-type: text/html; charset=UTF-8");
error_reporting(E_ALL);
define('CHARSET', 'ISO-8859-1');
define('REPLACE_FLAGS', ENT_COMPAT | ENT_XHTML);
class my_object{
//..... variables
}
$request = new Request(array());
if(isset($request->params)){
$array_r=$request->params;
$inputData->last_name=($array_r->last_name);
$inputData->first_name=($array_r->first_name);
$inputData->middle_name=($array_r->middle_name);
}
else{
//echo "Update of User failed";
$res = new Response();
$res->success = false;
$res->message = "Update User Failed!";
$res->data = array();
print_r($res->to_json());
}
?>作为参考,下面是我的Request.php文件
<?php
class Request {
public $method, $params;
public function __construct($params) {
// $this->restful = (isset($params["restful"])) ? $params["restful"] : false;
$this->method = $_SERVER["REQUEST_METHOD"];
$this->parseRequest();
}
protected function parseRequest() {
// grab JSON data if there...
$this->params = (isset($_REQUEST['data'])) ? json_decode(stripslashes($_REQUEST['data'])) : null;
if (isset($_REQUEST['data'])) {
$this->params = json_decode(stripslashes($_REQUEST['data']));
} else {
$raw = '';
$httpContent = fopen('php://input', 'r');
//print_r($httpContent);
while ($kb = fread($httpContent, 1024)) {
$raw .= $kb;
}
$params = json_decode(stripslashes($raw));
if ($params) {
$this->params = $params->data;
}
}
}
}
?>尽管我已经检查了ExtJS的文档,以及JSONWriter中的encode属性,但它显示:
Configure `true` to send record data (all record fields if writeAllFields is true) as a JSON Encoded HTTP Parameter named by the rootProperty configuration. 因此,我的模型数据是作为JSON编码的HTTP Parameter发送的,PHP能够解析它,因为rootProperty是data,它与我的request.php文件中的第19行匹配:
$this->params = (isset($_REQUEST['data'])) ? json_decode(stripslashes($_REQUEST['data'])) : null;我认为json_decode(stripslashes(...)) is the one causing‘应该转换为utf-8等效项,并且带斜杠删除了我们通常在utf-8中看到的前导反斜杠。
我有一种感觉,有一种更好的方法来写这段代码,而不是把ñ转换成utf-8。
有没有人有更好的解决方案?
发布于 2021-03-03 23:32:42
解决了它。问题出在request.php文件中的stripslash。
当JSON通过Store发送数据时,我使用encode,它将数据作为一个编码的ExtJS参数。通常,假设没有转义的特殊字符,当request.php文件获得它并运行json_decode时,这不是问题。问题是,当我点击ñ时,它被转换为\u00f1,并且前导斜杠被修剪掉了。json_decode可能没有再次识别它,因为没有前导斜杠。
在lead的评论中主要归功于IMSoP。
https://stackoverflow.com/questions/66454289
复制相似问题