Home >Backend Development >PHP Tutorial >mysql 碰到特殊字符,造成了数据不全

mysql 碰到特殊字符,造成了数据不全

WBOY
WBOYOriginal
2016-06-13 12:12:301104browse

mysql 遇到特殊字符,造成了数据不全
我是把数组序列化之后存入数据库的某个字段,但是数组中有些数据的值带有特殊字符,如图:

保存在数据库之后就成这样子了


类似这样的特殊字符还有很多的;

丢失了一部分数据,哪位哥哥遇到过这种情况吗?请赐教。
------解决思路----------------------

header('Content-type: text/html;charset=utf-8');<br />$s = '6b6Z5Y2O8K+gpQ==';<br />echo $t = base64_decode($s), PHP_EOL;<br />echo bin2hex($t);
龙华
------解决思路----------------------
把不是utf8的字符過濾就可以了。
<br />header('Content-type: text/html;charset=utf-8');<br />$s = '6b6Z5Y2O8K+gpTEyMzEyMzEyMw==';<br />$t = base64_decode($s). PHP_EOL;<br />echo $t.'<br>';<br />echo filter_utf8_char($t);<br /><br />/** 過濾字符串,保留UTF8字母數字中文及部份符號 <br />*   @param  String  $ostr <br />*   @return String <br />*/  <br />function filter_utf8_char($ostr){  <br />    preg_match_all('/[\x{FF00}-\x{FFEF}<br><font color='#FF8000'>------解决思路----------------------</font><br>\x{0000}-\x{00ff}<br><font color='#FF8000'>------解决思路----------------------</font><br>\x{4e00}-\x{9fff}]+/u', $ostr, $matches);  <br />    $str = join('', $matches[0]);  <br />    if($str==''){   //含有特殊字符需要逐個處理  <br />        $returnstr = '';  <br />        $i = 0;  <br />        $str_length = strlen($ostr);  <br />        while ($i<=$str_length){  <br />            $temp_str = substr($ostr, $i, 1);  <br />            $ascnum = Ord($temp_str);  <br />            if ($ascnum>=224){  <br />                $returnstr = $returnstr.substr($ostr, $i, 3);  <br />                $i = $i + 3;  <br />            }elseif ($ascnum>=192){  <br />                $returnstr = $returnstr.substr($ostr, $i, 2);  <br />                $i = $i + 2;  <br />            }elseif ($ascnum>=65 && $ascnum<=90){  <br />                $returnstr = $returnstr.substr($ostr, $i, 1);  <br />                $i = $i + 1;  <br />            }elseif ($ascnum>=128 && $ascnum<=191){ // 特殊字符  <br />                $i = $i + 1;  <br />            }else{  <br />                $returnstr = $returnstr.substr($ostr, $i, 1);  <br />                $i = $i + 1;  <br />            }  <br />        }  <br />        $str = $returnstr;  <br />        preg_match_all('/[\x{FF00}-\x{FFEF}<br><font color='#FF8000'>------解决思路----------------------</font><br>\x{0000}-\x{00ff}<br><font color='#FF8000'>------解决思路----------------------</font><br>\x{4e00}-\x{9fff}]+/u', $str, $matches);  <br />        $str = join('', $matches[0]);  <br />    }  <br />    return $str;  <br />}  <br />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn