Skip to content

Instantly share code, notes, and snippets.

@dtinth
Created November 26, 2010 14:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dtinth/716814 to your computer and use it in GitHub Desktop.
Save dtinth/716814 to your computer and use it in GitHub Desktop.
Utility function to fix floating tone marks when outputting as PDF.
<?php
function thai($x) {
$back = array(
"\xE0\xB9\x88" => "\xEF\x9C\x85",
"\xE0\xB9\x89" => "\xEF\x9C\x86",
"\xE0\xB9\x8A" => "\xEF\x9C\x87",
"\xE0\xB9\x8B" => "\xEF\x9C\x88",
"\xE0\xB9\x8C" => "\xEF\x9C\x89"
);
$cross = array();
foreach (array("\xE0\xB8\xB4", "\xE0\xB8\xB5", "\xE0\xB8\xB6", "\xE0\xB8\xB7") as $p) {
for ($i = 0x85; $i <= 0x89; $i ++) {
$from = $p . "\xEF\x9C" . chr($i);
$to = $p . "\xE0\xB9" . chr($i + 3);
$cross[$from] = $to;
}
}
$x = strtr($x, $back);
$x = strtr($x, $cross);
return $x;
}
@lex0000
Copy link

lex0000 commented Dec 26, 2014

function thai($x) {
$back = array(
"\xE0\xB9\x88" => "\xEF\x9C\x85", //่(ไม้เอก)=>
"\xE0\xB9\x89" => "\xEF\x9C\x86", //้(ไม้โทร)=>
"\xE0\xB9\x8A" => "\xEF\x9C\x87", //๊(ไม้ตรี)=>
"\xE0\xB9\x8B" => "\xEF\x9C\x88", //+(ไม้จัตวา)=>
"\xE0\xB9\x8C" => "\xEF\x9C\x89" //์(ตัวการัน)=>
);
$cross = array();
$payanchana=array(
"\xE0\xB8\xB4", //สระอิ
"\xE0\xB8\xB5", //สระอี
"\xE0\xB8\xB6", //สระอึ
"\xE0\xB8\xB7", //สระอือ
"\xE0\xB8\xB1", //ไม้หันอากาศ
"ำ" //สระอำ \xE0\xB8\xB3
);
//http://www.endmemo.com/unicode/thai.php อ้างอิง unicode
foreach ($payanchana as $p) {

        if($p!='ำ'){
    for ($i = 0x85; $i <= 0x89; $i ++) {
        $from = $p . "\xEF\x9C" . chr($i);
        $to   = $p . "\xE0\xB9" . chr($i +3);
        $cross[$from] = $to;

    }
        }else{
            for ($i = 0x85; $i <= 0x89; $i ++) {
        $from = "\xEF\x9C" . chr($i).$p;
        $to   = "\xE0\xB9" . chr($i +3).$p;
        $cross[$from] = $to;
    }
        }

}
    $x = strtr($x, $back);
    $x = strtr($x, $cross);


return $x;

}

@ton212
Copy link

ton212 commented Sep 27, 2018

ปรับให้วรรณยุกต์ที่ซ้อนอยู่บน อิ อี อึ อือ และไม้หันอากาศ อยู่ชิดขวาของตัวอักษร ให้เป็นธรรมชาติมากขึ้น

function thai($x)
{
    $back = array(
        "\xE0\xB9\x88" => "\xEF\x9C\x8A", // ไม้เอก
        "\xE0\xB9\x89" => "\xEF\x9C\x8B", // ไม้โท
        "\xE0\xB9\x8A" => "\xEF\x9C\x8C", // ไม้ตรี
        "\xE0\xB9\x8B" => "\xEF\x9C\x8D", // ไม้จัตวา
        "\xE0\xB9\x8C" => "\xEF\x9C\x8E" // ตัวการันต์
    );
    $cross = array();

    $vowels = array(
        "\xE0\xB8\xB4", // สระอิ
        "\xE0\xB8\xB5", // สระอี
        "\xE0\xB8\xB6", // สระอึ
        "\xE0\xB8\xB7", // สระอือ
        "\xE0\xB8\xB1", // ไม้หันอากาศ
        "" //สระอำ \xE0\xB8\xB3
    );
    //http://www.endmemo.com/unicode/thai.php อ้างอิง unicode

    foreach ($vowels as $p) {

        if ($p != 'ำ') {
            for ($i = 0x8A; $i <= 0x8E; $i++) {
                $from = $p . "\xEF\x9C" . chr($i);
                $to = $p . "\xE0\xB9" . chr($i - 2);
                $cross[$from] = $to;

            }
        } else {
            for ($i = 0x8A; $i <= 0x8E; $i++) {
                $from = "\xEF\x9C" . chr($i) . $p;
                $to = "\xE0\xB9" . chr($i - 2) . $p;
                $cross[$from] = $to;
            }
        }

    }
    $x = strtr($x, $back);
    $x = strtr($x, $cross);
    
    return $x;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment