kankai's avatar

Unicode problem, output strange words when in chinese

Hi guys,

I'm using summernote editor post content and upload image to my website. But i found out unicode problem and save strange words to my db.

常勇和舒森不仅是男神女神,也是超有才华的YouTuber哦!从高中毕业到大学毕业,两人都在彼此的身边!【高中到大å­

i suspect these code will affect to output but i'm not good with these

$detail=$request->input('detail');

$dom = new \DomDocument();

$dom->loadHtml($detail, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); 
        
$images = $dom->getElementsByTagName('img');

foreach($images as $k => $img){

            $data = $img->getAttribute('src');

            list($type, $data) = explode(';', $data);

            list(, $data)      = explode(',', $data);

            $data = base64_decode($data);

            $image_name= "/upload/" .time().$k.'.jpg';


            $path = public_path() . $image_name;

            file_put_contents($path, $data);

            $img->removeAttribute('src');

            $img->setAttribute('src', $image_name);

        }

        $detail = $dom->saveHTML();
0 likes
4 replies
bobbybouwmann's avatar

What kind of data do you put into the editor? Just regular chinese text and images?

kankai's avatar

@BOBBYBOUWMANN - yaya, i just type chinese text and images into it and db there come out these strange words

kankai's avatar

Hi Guys,

I found solution from google, it change the words unicode when i "loadHTML" , "saveHTML". I added in these code and solved it.

$doc->loadHTML('<?xml encoding="UTF-8">'. $detail);
 $detail = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
        $detail .= $doc->saveHTML( $doc->documentElement ); // important!

Please or to participate in this conversation.