sami
sami

Reputation: 49

php htmlentities corrupt string

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;
    }

Upvotes: 1

Views: 146

Answers (1)

saMuEl
saMuEl

Reputation: 50

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;
        }

Upvotes: 2

Related Questions