0

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?

Community
  • 1
  • 1
joakim.g
  • 71
  • 1
  • 6
  • 1
    Did you read what `mb_internal_encoding` does? It changes the default behavior of the other `mb_` functions. Of which you are not using any. Basically you need to modify `swap` to use multibyte functions instead (see [Wrong output when using array indexing on UTF-8 string](http://stackoverflow.com/q/6315750/50079)), and use `mb_strlen` instead of plain `strlen`. – Jon Aug 26 '14 at 14:01
  • Sorry, forgot to add that I tried with mb_substr which was suggested in another thread about Thai but that didn't do any difference so I removed it before writing the code here. I will try what you suggested, thanks! – joakim.g Aug 26 '14 at 14:06
  • Updated the code now, but still not quite there. Any ideas? – joakim.g Aug 26 '14 at 15:10

1 Answers1

1

The only significant change needed from the original is to modify the swap() function to build up a proper array of characters, rather than using array access on the string directly. (That and using mb_strlen() instead of strlen() to get the length of the string when calling permute().)


Working:

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 "$str\n";
    }
    else
    {
        for ($j = $i; $j < $n; $j++)
        {
            swap($str,$i,$j);
            permute($str, $i+1, $n);
            swap($str,$i,$j); // backtrack.
        }
    }
}

function swap(&$str,$i,$j) {
    $chars = array();
    for ($p = 0; $p < mb_strlen($str); $p++) {
        $chars[] = mb_substr($str, $p, 1);
    }
    $temp = $chars[$i];
    $chars[$i] = $chars[$j];
    $chars[$j] = $temp;
    $str = implode($chars);
}

$str = "สม";

permute($str, 0, mb_strlen($str)); // call the function.

Output:

สม
มส
Community
  • 1
  • 1
user3942918
  • 25,539
  • 11
  • 55
  • 67