<?php

function create_guid()
{
    $microTime = microtime();
    list ($a_dec, $a_sec) = explode(" ", $microTime);

    $dec_hex = sprintf("%x", $a_dec * 1000000);
    $sec_hex = sprintf("%x", $a_sec);

    ensure_length($dec_hex, 5);
    ensure_length($sec_hex, 6);

    $guid = "";
    $guid .= $dec_hex;
    $guid .= create_guid_section(3);
    $guid .= '-';
    $guid .= create_guid_section(4);
    $guid .= '-';
    $guid .= create_guid_section(4);
    $guid .= '-';
    $guid .= create_guid_section(4);
    $guid .= '-';
    $guid .= $sec_hex;
    $guid .= create_guid_section(6);

    return $guid;
}

function create_guid_section($characters)
{
    $return = "";
    for ($i = 0; $i < $characters; $i ++) {
        $return .= sprintf("%x", mt_rand(0, 15));
    }
    return $return;
}

function ensure_length(&$string, $length)
{
    $strlen = strlen($string);
    if ($strlen < $length) {
        $string = str_pad($string, $length, "0");
    } else
        if ($strlen > $length) {
            $string = substr($string, 0, $length);
        }
}

function ch($str)
{
    return iconv("IBM852", "UTF-8", $str);
}

function checkCharset($testString, $targetString)
{
    $out = false;
    $encoding = array(
        "ASCII",
        "ISO-8859-1",
        "ISO-8859-2",
        "ISO-8859-3",
        "ISO-8859-4",
        "ISO-8859-5",
        "ISO-8859-7",
        "ISO-8859-9",
        "ISO-8859-10",
        "ISO-8859-13",
        "ISO-8859-14",
        "ISO-8859-15",
        "ISO-8859-16",
        "KOI8-R",
        "KOI8-U",
        "KOI8-RU",
        "CP1250",
        "CP1251",
        "CP1252",
        "CP1253",
        "CP1254",
        "CP1257",
        "CP850",
        "CP866",
        "Mac Roman",
        "Mac CentralEurope",
        "Mac Iceland",
        "Mac Croatian",
        "Mac Romania",
        "Mac Cyrillic",
        "Mac Ukraine",
        "Mac Greek",
        "Mac Turkish",
        "Macintosh",
        "ISO-8859-6",
        "ISO-8859-8",
        "CP1255",
        "CP1256",
        "CP862",
        "Mac Hebrew",
        "Mac Arabic",
        "EUC-JP",
        "SHIFT_JIS",
        "CP932",
        "ISO-2022-JP",
        "ISO-2022-JP-2",
        "ISO-2022-JP-1",
        "EUC-CN",
        "HZ",
        "GBK",
        "GB18030",
        "EUC-TW",
        "BIG5",
        "CP950",
        "BIG5-HKSCS",
        "ISO-2022-CN",
        "ISO-2022-CN-EXT",
        "EUC-KR",
        "CP949",
        "ISO-2022-KR",
        "JOHAB",
        "ARMSCII-8",
        "Georgian-Academy",
        "Georgian-PS",
        "KOI8-T",
        "TIS-620",
        "CP874",
        "MacThai",
        "MuleLao-1",
        "CP1133",
        "VISCII",
        "TCVN",
        "CP1258",
        "HP-ROMAN8",
        "NEXTSTEP",
        "UTF-8",
        "UCS-2",
        "UCS-2BE",
        "UCS-2LE",
        "UCS-4",
        "UCS-4BE",
        "UCS-4LE",
        "UTF-16",
        "UTF-16BE",
        "UTF-16LE",
        "UTF-32",
        "UTF-32BE",
        "UTF-32LE",
        "UTF-7",
        "C99",
        "JAVA",
        "UCS-2-INTERNAL",
        "UCS-4-INTERNAL",
        "CP437",
        "CP737",
        "CP775",
        "CP850",
        "CP852",
        "CP853",
        "CP855",
        "CP857",
        "CP858",
        "CP860",
        "CP861",
        "CP863",
        "CP865",
        "CP869",
        "CP1125",
        "CP864",
        "EUC-JISX0213",
        "Shift_JISX0213",
        "ISO-2022-JP-3",
        "TDS565",
        "RISCOS-LATIN1"
    );

    foreach ($encoding as $v) {
        if (iconv($v, "utf-8", $testString) === $targetString) {
            $out = $v;
        }
    }

    return $out;
}