FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
EncodingConverter.php
1 <?php
2 /*
3 Copyright (C) 2014, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 namespace Fossology\Lib\Text;
20 
21 
22 class EncodingConverter implements Converter
23 {
24  const UTF8_ENCODING = "UTF-8";
25 
30  function convert($input)
31  {
32  if ($this->isUtf8($input)) {
33  return $input;
34  } else {
35  $encodings = array("ASCII", "UTF-8", "Windows-1252", "ISO-8859-15", "ISO-8859-1", "GB2312");
36  $detectedCharset = mb_detect_encoding($input, $encodings, true);
37 
38  if (!$detectedCharset) {
39  $charsets = array('iso-8859-1', 'windows-1251', 'GB2312');
40  foreach ($charsets as $charset) {
41  $output = iconv($charset, self::UTF8_ENCODING . '//TRANSLIT', $input);
42  if ($output) {
43  return $output;
44  }
45  }
46  } else {
47  return iconv($detectedCharset, self::UTF8_ENCODING, $input);
48  }
49  }
50  }
51 
52  public function isUtf8($input)
53  {
54  return mb_check_encoding($input, self::UTF8_ENCODING);
55  }
56 }