dse3168 2012-04-17 12:17
浏览 80
已采纳

将php数据发送到textarea - 编码问题

I've using two textareas and a button. The idea is when the button is pressed all the information on textarea1 will be send to the php function reverse them and then return the result on textarea2 (I am using ajax as well so I don't have to reload the website).

Now the issue is, although this works with properly there is an issue with special symbols since they show up as � when they come back in textarea2 (not all of them though) so I'm assuming there is an issue with the encoding somewhere.

This is the simple php code that returns the result to textarea2

<?php
$data = rawurldecode($_GET["data"]);

//mb_internal_encoding("UTF-8");
//mb_http_output( "UTF-8" );
// ob_start("mb_output_handler");

echo strrev($data);
?>

As you can see I tried already to set the internal encoding to UTF-8, also I've tried encoding the data before they get send to php and decode them in the php function but it had the same effect.

  • 写回答

1条回答 默认 最新

  • doujuncuo9339 2012-04-17 12:18
    关注

    strrev() is going to reverse the sequence of bytes which isn't good news for variable byte length encodings such as UTF-8. Characters that can be represented with one byte (such as ASCII) in UTF-8 are safe, but ones which are not are going to be mangled.

    You can use a regular expression with the Unicode flag enabled to get around this limitation of strrev().

    // `u` flag so `.` match Unicode characters and `s` flag so `.` matches `
    `.
    preg_match_all('/./us', $str, $matches);
    // Reverse the array of matches, and then join the characters back together.
    $str = implode(array_reverse($matches[0]));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题