title: PHP: Convert any string to UTF-8 without knowing the original character set, or at least try
body: <p>I have an application that deals with clients from all over the world, and, naturally, I want everything going into my databases to be UTF-8 encoded.</p>
  <p>The main problem for me is that I don't know what encoding the source of any string is going to be - it could be from a text box (using <code>&lt;form accept-charset=&quot;utf-8&quot;&gt;</code> is only useful if the user is actually submitted the form), or it could be from an uploaded text file, so I really have no control over the input.</p>
  <p>What I need is a function or class that makes sure the stuff going into my database is, as far as is possible, UTF-8 encoded. I've tried <code>iconv(mb_detect_encoding($text), &quot;UTF-8&quot;, $text);</code>
  but that has problems (if the input is 'fiancée' it returns 'fianc'). I've tried a lot of things =/</p>
  <p>For file uploads, I like the idea of asking the end user to specify the encoding they use, and show them previews of what the output will look like, but this doesn't help against nasty hackers (in fact, it could make their life a little easier).</p>
  <p>I've read the other Stack Overflow questions on the subject, but they seem to all have subtle differences like &quot;I need to parse <a href="https://en.wikipedia.org/wiki/RSS" rel="nofollow noreferrer">RSS</a> feeds&quot; or &quot;I scrape data from websites&quot; (or, indeed, &quot;You can't&quot;).</p>
  <p>But there must be something that at least has a good <em>try</em>!</p>