FOSSology  4.4.0
Open Source License Compliance by Open Source Software
common-string.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4  Author: D.Fognini, S. Weber, J.Najjar
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
14 // For compatibility with older php versions
15 if (! defined('ENT_SUBSTITUTE')) {
16  define('ENT_SUBSTITUTE', 0);
17 }
18 
25 function convertToUTF8($content, $toHTML=true)
26 {
27  if (strlen($content) == 0) {
28  return '';
29  }
30  if (checkUTF8($content)) {
31  $output1 = $content;
32  } else {
33  $output1 = tryConvertToUTF8($content);
34  if (! $output1 || ! checkUTF8($output1)) {
35  $output1 = $toHTML ? "<Unknown encoding>" : "<b>Unknown encoding</b>";
36  }
37  }
38 
39  if (! $toHTML) {
40  return $output1;
41  }
42  return (htmlspecialchars($output1, ENT_SUBSTITUTE, "UTF-8")) ?: "<b>Unknown encoding</b>";
43 }
44 
50 function checkUTF8($content)
51 {
52  return mb_check_encoding($content, "UTF-8");
53 }
54 
60 function tryConvertToUTF8($content)
61 {
62  $inCharset = mb_detect_encoding($content, mb_detect_order(), true);
63  $output1 = false;
64  if (! $inCharset) {
65  $charsets = array('iso-8859-1', 'windows-1251', 'GB2312');
66  foreach ($charsets as $charset) {
67  $output1 = iconv($charset, "UTF-8", $content);
68  if ($output1) {
69  break;
70  }
71  }
72  } else if ($inCharset != "UTF-8") {
73  $output1 = iconv($inCharset, "UTF-8", $content);
74  }
75  return $output1;
76 }
tryConvertToUTF8($content)
checkUTF8($content)
if(! defined('ENT_SUBSTITUTE')) convertToUTF8($content, $toHTML=true)