title: charset issue with accents retrieving DB2 values with php over odbc
body: <p>I'm trying to do a select from a DB2 through PHP and odbc and then save those values on a file. The OS where the code is being executed is Debian. What I do is the following:</p>
  
  <pre><code>$query = "SELECT NAME FROM DATABASE_EXAMPLE.TABLE_EXAMPLE";
  $result = odbc_prepare($server, $query);
  $success = odbc_execute($result);
  $linias = "";
  
  if ($success) {
      while ($myRow = odbc_fetch_array($result)) {
          $linias .=format_word($myRow['NAME'], 30) . "\r\n";  
  }
  
  generate_file($linias);
  
  
  function format_word($paraula, $longitut) {
      return str_pad(utf8_encode($paraula), $longitut, " ", STR_PAD_LEFT);
  }
  
  function generate_file($linias) {
  
      $nom_fitxer = date('YmdGis');
      file_put_contents($nom_fitxer . ".tmp", $linias);
      rename($nom_fitxer . '.tmp', $nom_fitxer . '.itf');
  
  }
  </code></pre>
  
  <p>The problem is that some of the retrieved values contains spanish letters and accents. To make and example, one of the values is "ÁNGULO". If I var_dump the code on my browser I get the word fine, but when it's write into the file it apends weird characters on it (that's why I think there is a problem with the charset). I have tried different workarounds but it just make it worst. The file opened with Notepad++ (with UTF8 encoding enabled) looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/JJSe2.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JJSe2.jpg" alt="Notepad++ capture"></a></p>
  
  <p>Is there a function in PHP that translate between charsets?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Following erg instructions I do further research:</p>
  
  <p>The DB2 database use IBM284 charset, as I found executing the next command:</p>
  
  <pre><code>select table_schema, table_name, column_name, character_set_name from SYSIBM.COLUMNS
  </code></pre>
  
  <p>Firefox says the page is encoded as Unicode.
  If i do: </p>
  
  <pre><code>var_dump(mb_detect_encoding($paraula));
  </code></pre>
  
  <p>I get bool(false) as a result.</p>
  
  <p>I have changed my function for formating the word hoping that iconv resolve the conflict:</p>
  
  <pre><code>function format_word($paraula, $longitut) {
      $paraula : mb_convert_encoding($paraula, 'UTF-8');
      $paraula= iconv("IBM284", "UTF-8", $paraula);
      return $paraula;
  }
  </code></pre>
  
  <p>But it doesn't. Seems like the ODBC it's doing some codification bad and that is what mess the data. How can I modify the odbc to codificate to the right charset? I have seen some on Linux changing the locale, but if I execute the command locale on the PC I get:</p>
  
  <pre><code>LC_NAME="es_ES.UTF-8"
  LC_ADDRESS="es_ES.UTF-8"
  ...
  </code></pre>