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 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题