Search code examples
phpthai

Permutations of a string with thai characters in php


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?


Solution

  • 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:

    สม
    มส