I was reading this thread: How to generate all permutations of a string in PHP? and I want to do the same thing but with Thai characters.. but all I'm getting is lots of weird characters. Must be something with the encoding? I am using UTF-8.
Here is the code (originally from the thread mentioned above):
<?php
mb_internal_encoding('UTF-8');
// function to generate and print all N! permutations of $str. (N = strlen($str)).
function permute($str,$i,$n)
{
if ($i == $n)
{
print mb_substr($str, 0);
print "<br></br>";
}
else
{
for ($j = $i; $j < $n; $j++)
{
swap((mb_substr($str, 0)),$i,$j);
permute((mb_substr($str, 0)), $i+1, $n);
swap($str,$i,$j); // backtrack.
}
}
}
// function to swap the char at pos $i and $j of $str.
function swap(&$str,$i,$j)
{
$temp = $str[$i];
$str[$i] = $str[$j];
$str[$j] = $temp;
}
$str = "สม";
permute((mb_substr($str, 0)),0,mb_strlen($str)); // call the function.
?>
Code updated, the print out now is like this:
สม
สม
What am I missing?