0

OK, first, I have read UTF-8 all the way through and followed every suggested solution there, including those in this linked document. However, I'm still getting some kind of encoding-driven problems. My test code now looks like this:

<?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>

The only row in the "test" table has the following Content value:

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£

which contains the string £LATEST_NEWS£. However, the substitution doesn't occur (what is echoed is exactly the value of the Content table).

The database collation is set to utf8mb4_general_ci, the file is encoded in UTF8 (saved as such with Notepad++, confirmed via checking encoding of a string literal). Any ideas what I might have missed?

user3482749
  • 113
  • 3
  • 1
    I wouldn't use `utf8_encode()`, because that requires a ISO-8859-1 charset, specifically, so unless you can guarantee that charset, using that function might just break things even more. Did you save the document with UTF8 or UTF8 w/o BOM (in Notepad++)? And what is the charset of the table (collation is *not* the same as charset). I have previously written [**an answer about UTF-8 encoding**](https://stackoverflow.com/a/31899827/4535200) that contains a little checklist, that will cover *most* of the charset issues in a PHP/MySQL application, check that out - perhaps you missed something. – Qirel Sep 03 '17 at 20:03
  • See https://stackoverflow.com/questions/38363566/trouble-with-utf-8-characters-what-i-see-is-not-what-i-stored – Rick James Sep 04 '17 at 03:20

0 Answers0