title: XML encoding error, but both XML and input text encoding are utf-8 in php
body: <p>I'm generating a XML Dom with DomDocument in php, containing some news, with title, date, links and a description. The problem occurs on description of some news, but not on others, and both of them contains accents and cedilla.</p>
  
  <p>I create the XML Dom element in UTF-8:</p>
  
  <p><code>$dom = new \DOMDocument("1.0", "UTF-8");</code></p>
  
  <p>Then, I retrieve my text from a MySQL database, which is encoded in latin-1, and after I tested the encoding with <code>mb_detect_encoding</code> it returns UTF-8.</p>
  
  <p>I tried the following:</p>
  
  <pre><code>iconv('UTF-8', 'ISO-8859-1', $descricao);
  iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $descricao);
  iconv('ISO-8859-1', 'UTF-8', $descricao);
  iconv('ISO-8859-1//TRANSLIT', 'UTF-8', $descricao);
  mb_convert_encoding($descricao, 'ISO-8859-1', 'UTF-8');
  mb_convert_encoding($descricao, 'UTF-8', 'ISO-8859-1');
  mb_convert_encoding($descricao, 'UTF-8', 'UTF-8'); //that makes no sense, but who knows
  </code></pre>
  
  <p>Also tried changing the database encode to UTF-8, and changing the XML encode to ISO-8859-1.</p>
  
  <p>This is the full method that generates the XML:</p>
  
  <pre><code>$informativos = Informativo::where('inf_ativo','S')-&gt;orderBy('inf_data','DESC')-&gt;take(20)-&gt;get();
  $dom = new \DOMDocument("1.0", "UTF-8");
  $dom-&gt;preserveWhiteSpace = false;
  $dom-&gt;formatOutput = true;
  $rss = $dom-&gt;createElement("rss");
  
  $channel = $dom-&gt;createElement("channel");
  $title = $dom-&gt;createElement("title", "Informativos");
  $link = $dom-&gt;createElement("link", "http://example.com/informativos");
  
  $channel-&gt;appendChild($title);
  $channel-&gt;appendChild($link);
  
  foreach ($informativos as $informativo) {
      $item = $dom-&gt;createElement("item");
  
      $itemTitle = $dom-&gt;createElement("title", $informativo-&gt;inf_titulo);
      $itemImage = $dom-&gt;createElement("image", "http://example.com/".$informativo-&gt;inf_ilustracao);
      $itemLink = $dom-&gt;createElement("link", "http://example.com/informativo/".$informativo-&gt;informativo_id);
      $descricao = strip_tags($informativo-&gt;inf_descricao);
      $descricao = str_replace("&amp;nbsp;", " ", $descricao);
      $descricao = str_replace("&amp;#13;", " ", $descricao);
      $descricao = substr($descricao, 0, 150).'...';
      $itemDesc = $dom-&gt;createElement("description", $descricao);
      $itemDate = $dom-&gt;createElement("pubDate", $informativo-&gt;inf_data);
  
      $item-&gt;appendChild($itemTitle);
      $item-&gt;appendChild($itemImage);
      $item-&gt;appendChild($itemLink);
      $item-&gt;appendChild($itemDesc);
      $item-&gt;appendChild($itemDate);
  
      $channel-&gt;appendChild($item);
  }
  
  $rss-&gt;appendChild($channel);
  
  $dom-&gt;appendChild($rss);
  
  return $dom-&gt;saveXML();
  </code></pre>
  
  <p>Here is an example of successful output:</p>
  
  <p><code>Segundo a instituição, número de pessoas que vivem na pobreza subiu 7,3 milhões desde 2014, atingindo 21% da população, ou 43,5 milhões de br</code></p>
  
  <p>And an example that gives the encoding error:</p>
  
  <pre><code>procuradores da Lava Jato em Curitiba, que estão sendo investigados por um&amp;#13;
  suposto acordo fraudulento com a Petrobras e o Departamento de Justi�...
  </code></pre>
  
  <p>Everything renders fine, until the problematic description text above, that gives me:</p>
  
  <p>"This page contains the following errors:
  error on line 118 at column 20: Encoding error
  Below is a rendering of the page up to the first error."</p>
  
  <p>Probably that <code>&amp;#13;</code> is the problem here. Since I can't control whether or not the text have this, I need to render these special characters correctly.</p>
  
  <p><strong>UPDATE 2019-04-12:</strong> Found out the error on the problematic text and changed the example.</p>