解决方案:
<codelucida console mono sans vera new white-space:inherit><span>$str </span><span>=</span><span> mb_convert_encoding</span><span>(</span><span>$str</span><span>,</span><span></span><span>"UTF-8"</span><span>,</span><span></span><span>"Windows-1252"</span><span>);</span><phelvetica neue line-height:19.5px>Another function in PHP that can convert the encoding / charset of a string is called <codelucida console mono sans vera new white-space:pre-wrap background-color:rgb>iconv based
onlibiconv. You can use it as well:<pre class="brush:php;toolbar:false;" lucida console mono sans vera new color:rgb word-wrap:normal margin-bottom:5px overflow:auto background-color:rgb><codelucida console mono sans vera new white-space:inherit><span>$str </span><span>=</span><span> iconv</span><span>(</span><span>"CP1252"</span><span>,</span><span></span><span>"UTF-8"</span><span>,</span><span> $str</span><span>);</span><h3helvetica neue>Note on utf8_encode()<phelvetica neue line-height:19.5px><codelucida console mono sans vera new white-space:pre-wrap background-color:rgb>utf8_encode() does
only work for <codelucida console mono sans vera new white-space:pre-wrap background-color:rgb>Latin-1,
not for <codelucida console mono sans vera new white-space:pre-wrap background-color:rgb>ANSI.
So you will destroy part of your characters inside that string when you run it through that function.<hrhelvetica neue font-size:15px line-height:19.5px background-color:rgb><phelvetica neue line-height:19.5px>Related: What is ANSI format?<hrhelvetica neue font-size:15px line-height:19.5px background-color:rgb><phelvetica neue line-height:19.5px>For a more fine-grained control of what <codelucida console mono sans vera new white-space:pre-wrap background-color:rgb>json_encode() returns,
see the list of predifined constants(PHP version dependent, incl. PHP 5.4, some constants
remain undocumented and are available in the source code only so far).<h2helvetica neue>Changing the encoding of an array/iteratively (PDO comment)<phelvetica neue line-height:19.5px>As you wrote in a comment that you have problems to apply the function onto an array, here is some code example. It's <span>always</span> needed to <span>first</span> change
the encoding before using <codelucida console mono sans vera new white-space:pre-wrap background-color:rgb>json_encode.
That's just a standard array operation, for the simpler case of <codelucida console mono sans vera new white-space:pre-wrap background-color:rgb>pdo::fetch() a <codelucida console mono sans vera new white-space:pre-wrap background-color:rgb>foreach iteration:<divhelvetica neue line-height:19.5px><pre class="brush:php;toolbar:false;" lucida console mono sans vera new color:rgb word-wrap:normal margin-bottom:5px overflow:auto background-color:rgb><codelucida console mono sans vera new white-space:inherit><span>while</span><span>(</span><span>$row </span><span>=</span><span> $q</span><span>-></span><span>fetch</span><span>(</span><span>PDO</span><span>::</span><span>FETCH_ASSOC</span><span>))</span><span></span><span>{</span><span></span><span>foreach</span><span>(</span><span>$row </span><span>as</span><span></span><span>&</span><span>$value</span><span>)</span><span></span><span>{</span><span>
$value </span><span>=</span><span> mb_convert_encoding</span><span>(</span><span>$value</span><span>,</span><span></span><span>"UTF-8"</span><span>,</span><span></span><span>"Windows-1252"</span><span>);</span><span></span><span>}</span><span>
unset</span><span>(</span><span>$value</span><span>);</span><span></span><span># safety: remove reference</span><span>
$items</span><span>[]</span><span></span><span>=</span><span> array_map</span><span>(</span><span>'utf8_encode'</span><span>,</span><span> $row </span><span>);</span><span></span><span>}</span><br><p>项目中遇到的问题,记录以备后用 .</p>
<p>文献:</p>
<p>json_encode() non utf-8 strings<br></p><div class="aritcle_card flexRow">
<div class="artcardd flexRow">
<a class="aritcle_card_img" href="/ai/882" title="Olli.ai"><img
src="https://img.php.cn/upload/ai_manual/001/503/042/68b6d4de59ab4112.png" alt="Olli.ai" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a href="/ai/882" title="Olli.ai">Olli.ai</a>
<p>从web或文件数据快速创建数据可视化</p>
</div>
<a href="/ai/882" title="Olli.ai" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a>
</div>
</div>
<div>
<p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>
<p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p>
<p>
以上就介绍了php json_encode 数据,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。</p>
<p>
</p>
</div></codelucida>










