PHP MySQLi编码乱七八糟

时间:2017-09-03 19:31:12

标签: php mysql mysqli utf-8 character-encoding

好的,首先,我已阅读UTF-8 all the way through并遵循其中的所有建议解决方案,包括this linked document中的解决方案。但是,我仍然遇到某种编码驱动的问题。我的测试代码现在看起来像这样:

<?php header('Content-Type: text/html; charset=UTF-8'); ?>
<html><head>  <meta charset="UTF-8" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>
<?
    $conn = new mysqli($servername, $username, $password, $db); // obviously with these set properly in the real code
        mysqli_set_charset($conn, "utf8mb4");
        mb_language('uni'); 
        mb_internal_encoding('UTF-8');
        $conn->query("SET NAMES 'utf8'");
        $res = $conn->query("SELECT * FROM test");
        $row = $res->fetch_assoc(); 
        $content = $row['Content'];
if(mb_detect_encoding($content) != 'UTF-8') {          $content = utf8_encode($content); }


$content = str_replace("£LATEST_NEWS£", "SUCCESS", $content); 
$content = str_replace(utf8_encode("£LATEST_NEWS£"), "SUCCESS", $content); // just trying all the options in case I've horribly misunderstood something
$content = str_replace(utf8_decode("£LATEST_NEWS£"), "SUCCESS", $content); // just trying all the options in case I've horribly misunderstood something

echo $content; 

?></body></html>

“test”表中唯一的行具有以下内容值:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et dolor id neque suscipit rhoncus sed a purus. Vestibulum pellentesque erat sed nisi pulvinar, quis accumsan diam sollicitudin. Integer dignissim lectus vitae erat hendrerit, a maximus urna accumsan. Curabitur sit amet orci pulvinar, porta mauris lobortis, blandit sem. Etiam varius odio at felis tristique, eget tempor nisi auctor. Donec id mollis est. Curabitur ut lorem scelerisque, molestie purus nec, condimentum nulla. Sed vulputate fringilla tincidunt. Pellentesque eget facilisis leo, at ornare tortor. Nulla a feugiat arcu, ut faucibus sem. Cras vehicula consectetur est ac gravida. Pellentesque aliquet nulla id leo vulputate dapibus. Curabitur fermentum blandit diam quis lobortis. Quisque dapibus faucibus eleifend. Fusce rhoncus augue nec nisl volutpat cursus. Fusce quis blandit orci, vitae efficitur orci. Ut sodales aliquet lacinia. Donec et sagittis quam, nec accumsan massa. Donec non diam dolor. Maecenas scelerisque arcu eget pharetra rutrum. Sed mollis posuere risus, non sodales lacus gravida eu. £SLIDESHOW£ £LATEST_NEWS£

其中包含字符串£LATEST_NEWS£。但是,不会发生替换(回显的内容恰好是Content表的值)。

数据库排序规则设置为utf8mb4_general_ci,文件以UTF8编码(使用Notepad ++保存,通过检查字符串文字的编码来确认)。我可能错过了什么想法?

0 个答案:

没有答案