duanqun9740 2014-12-04 17:48
浏览 29
已采纳

php htmlentities损坏字符串

For security reasons I wanted to add a function to turn strings to safe format by using the code below. in normal English characters it works fine, but when i use Amharic characters like ከበደ I am getting different string like ከበደ, what shall I do.

    echo safestring("ከበደ");
    //the string after echo is absolutely changed


    function safestring($str){

        //make the string from SQL injection
        $str  = htmlentities($str);
        $str= mysql_real_escape_string($str);
        return $str;
    }
  • 写回答

1条回答 默认 最新

  • dtpw54085 2014-12-04 18:02
    关注

    First thing first, first you have provide the charset for your document

    HTML

    just add the following code the <head> element of your HTML

             <meta charset="UTF-8">
    

    PHP

    for json you can use the header function of php like so

             header('content-type: application/json; charset=utf-8');
    

    In order to prevent from losing any chars from the string you can use the code below

            function safestring($string){
                $string = trim($string);
                $string = str_replace("<", "&lt;", $string);
                $string = str_replace(">", "&gt;", $string);
                $string = mysql_real_escape_string($string);
                return  $string;
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化