FOSSology  4.4.0
Open Source License Compliance by Open Source Software
EncodingConverter.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Text;
9 
10 
11 class EncodingConverter implements Converter
12 {
13  const UTF8_ENCODING = "UTF-8";
14 
19  function convert($input)
20  {
21  if ($this->isUtf8($input)) {
22  return $input;
23  } else {
24  $encodings = array("ASCII", "UTF-8", "Windows-1252", "ISO-8859-15", "ISO-8859-1", "GB2312");
25  $detectedCharset = mb_detect_encoding($input, $encodings, true);
26 
27  if (!$detectedCharset) {
28  $charsets = array('iso-8859-1', 'windows-1251', 'GB2312');
29  foreach ($charsets as $charset) {
30  $output = iconv($charset, self::UTF8_ENCODING . '//TRANSLIT', $input);
31  if ($output) {
32  return $output;
33  }
34  }
35  } else {
36  return iconv($detectedCharset, self::UTF8_ENCODING, $input);
37  }
38  }
39  }
40 
41  public function isUtf8($input)
42  {
43  return mb_check_encoding($input, self::UTF8_ENCODING);
44  }
45 }