title: PHP JSON_encode() is getting "Malformed UTF-8 characters, possibly incorrectly encoded" (error)
body: <p>I cannot solve this issue and I'm getting crazy.</p>
  
  <p><code>JSON_encode()</code> is casting the error: <code>Malformed UTF-8 characters, possibly incorrectly encoded</code> on few records (2 or 3) from a set of 10k records.
  However this seems very impossible to fix.</p>
  
  <ul>
  <li>mysql is already utf8mb4 everywhere (database, table, columns and collation)</li>
  <li>php is 7.2 and of course in utf8</li>
  <li>apache default charset is utf8 (however the error is throw at PHP-level).</li>
  </ul>
  
  <p>I can also print to screen correctly the record in PHP without issue in a simple HTML debug page. However If I try to encode it in JSON I get the error.</p>
  
  <p>I found that these records have been imported from a CVS probably bypassing the cleaner. What is so strange is that the entire CSV file is parsed with:</p>
  
  <pre><code>$this-&gt;encoding = mb_detect_encoding($source,mb_detect_order(),true);
  if ($this-&gt;encoding!="" &amp;&amp; $this-&gt;encoding!="UTF8") {
      $source = iconv($this-&gt;encoding, "UTF-8", $source);
  } 
  </code></pre>
  
  <p>I cannot post any full broken data due to the privacy (and GDPR).
  However I succeed to extract a part which seems to be the broken one:</p>
  
  <pre><code>RESIDENCE �PRINCIPE
  </code></pre>
  
  <p><strong>UPDATES</strong></p>
  
  <p>I try to get the bitcode of these broken chars. This is what I found.
  In ASCII by using simple native function <code>str_split</code> and <code>ord</code> these char is:</p>
  
  <pre><code>'�' 160
  </code></pre>
  
  <p>I would like to find the bitcode also in utf8, so I find this usefull function on PHP.net <a href="http://php.net/manual/en/function.ord.php#109812" rel="noreferrer">http://php.net/manual/en/function.ord.php#109812</a>
  Which try to find bitcode of MultiByteStrings. and it gives me:</p>
  
  <pre><code>-2096
  </code></pre>
  
  <p>Which is....... negative?</p>