title: Detect encoding and make everything UTF-8
body: <p>I'm reading out lots of texts from various RSS feeds and inserting them into my database.</p>
  
  <p>Of course, there are several different character encodings used in the feeds, e.g. UTF-8 and ISO&nbsp;8859-1.</p>
  
  <p>Unfortunately, there are sometimes problems with the encodings of the texts. Example:</p>
  
  <ol>
  <li><p>The "ß" in "Fußball" should look like this in my database: "ÂŸ". If it is a "ÂŸ", it is displayed correctly.</p></li>
  <li><p>Sometimes, the "ß" in "Fußball" looks like this in my database: "ÃƒÂŸ". Then it is displayed wrongly, of course.</p></li>
  <li><p>In other cases, the "ß" is saved as a "ß" - so without any change. Then it is also displayed wrongly.</p></li>
  </ol>
  
  <p>What can I do to avoid the cases 2 and 3?</p>
  
  <p>How can I make everything the same encoding, preferably UTF-8? When must I use <code>utf8_encode()</code>, when must I use <code>utf8_decode()</code> (it's clear what the effect is but when must I use the functions?) and when must I do nothing with the input?</p>
  
  <p>How do I make everything the same encoding? Perhaps with the function <code>mb_detect_encoding()</code>? Can I write a function for this? So my problems are:</p>
  
  <ol>
  <li>How do I find out what encoding the text uses?</li>
  <li>How do I convert it to UTF-8 - whatever the old encoding is?</li>
  </ol>
  
  <p>Would a function like this work?</p>
  
  <pre><code>function correct_encoding($text) {
      $current_encoding = mb_detect_encoding($text, 'auto');
      $text = iconv($current_encoding, 'UTF-8', $text);
      return $text;
  }
  </code></pre>
  
  <p>I've tested it, but it doesn't work. What's wrong with it?</p>