Why do quotes turn into funny characters when submitted in an HTML form?

后端 未结 3 1535
你的背包
你的背包 2020-12-15 06:59

I have an HTML form, and some users are copy/pasting text from MS Word. When there are single quotes or double quotes, they get translated into funny characters like:

<
3条回答
  •  一生所求
    2020-12-15 07:27

    We have a PHP function that tries to clean up the mess with smart quotes. It's a bit of a mess, since it's grown a bit organically as cases popped up during prototype development. It may be of some help, though:

    function convert_smart_quotes($string) {
        $search = array(chr(0xe2) . chr(0x80) . chr(0x98),
                        chr(0xe2) . chr(0x80) . chr(0x99),
                        chr(0xe2) . chr(0x80) . chr(0x9c),
                        chr(0xe2) . chr(0x80) . chr(0x9d),
                        chr(0xe2) . chr(0x80) . chr(0x93),
                        chr(0xe2) . chr(0x80) . chr(0x94),
                        chr(226) . chr(128) . chr(153),
                        '’','“','â€<9d>','â€"','  ');
    
         $replace = array("'","'",'"','"',' - ',' - ',"'","'",'"','"',' - ',' ');
    
        return str_replace($search, $replace, $string);
    }
    

提交回复
热议问题