HtmGem is Php program whose goal is to make Gemini files reachable through HTTP. It can be used on a shared host. https://gmi.sbgodin.fr/htmgem
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

35 lines
1001 B

<?php declare(strict_types=1);
namespace htmgem\io;
define("_BOMS", array( // Byte Order Mark
// https://www.unicode.org/faq/utf_bom.html
"UTF-32LE" => "\xFF\xFE\x00\x00",
"UTF-16LE" => "\xFF\xFE",
"UTF-16BE" => "\xFE\xFF",
"UTF-8" => "\xEF\xBB\xBF",
"UTF-32BE" => "\x00\x00\xFE\xFF"
));
/**
* Returns the encoding among Unicode ones, using the BOM
* @param txt $text
* @returns the encoding, or UTF-8 if no BOM read
*/
function _detectUnicodeEncoding(&$text) {
/* The PHP built-in function mb-detect-encoding()
* doesn't detect UTF-16.
*/
foreach (_BOMS as $bomName => $bomBytes)
if (strpos($text, $bomBytes) === 0) return $bomName;
return "UTF-8";
}
/** Converts to UTF8 an Unicode text and removes the BOM
*/
function convertToUTF8(&$text) {
$encoding = _detectUnicodeEncoding($text);
$text = mb_convert_encoding($text, "UTF-8", $encoding);
$text = preg_replace("/^"._BOMS['UTF-8']."/", "", $text);
return $encoding;
}