1

我正在使用 PHP 进行一些梵文编码。我将给出规则的示例:规则是当 $kantha-$talu-$murdha 出现在相同的序列中时,$talu 是可选的重复。例如 aif -> aif。以下脚本为我提供了输出,以防只有一种此类事件发生。但是,如果字符串中出现多个此类事件,则脚本会失败。例如 $text = "aifkcw" 下面的脚本只会给出 "aiifkccw"。

在这里,由于可选性,应该有四个期望的结果。“aifkcw” - 没有变化。“aiifkcw” - 第一次出现重复,第二次保持不变。“aifkccw” - 第二次出现重复,第一次保持不变。"aiifkccw" - 两次出现重复。

我也尝试了一些正则表达式,但没有任何用处。任何帮助,将不胜感激。

<?php
$kantha = array("a","A","k","K","g","G","N","h","H");
$talu = array("i","I","c","C","j","J","Y","y","S");
$murdha = array("f","F","w","W","q","Q","R","r","z");
$text = "aifkcw";
echo $text."</br>";
for($i=0;$i<count($kantha);$i++)
{
for($j=0;$j<count($talu);$j++)
{
for($k=0;$k<count($murdha);$k++)
{
$text = str_replace($kantha[$i].$talu[$i].$murdha[$i],$kantha[$i].$talu[$j].$talu[$j].$murdha[$k],$text);
}
}
}
echo $text."</br>";
?>
4

1 回答 1

0

所以...我取出了区分大小写的部分,然后我意识到这可能是你的规则的一部分,所以你可能需要重新添加它。我需要去上班,所以我没有时间解释所有这些。我需要使用数组指针,因此您可能需要阅读next()current()

编辑:我添加了很多评论来尝试帮助您了解这里发生的事情。

<?php


// this is an array of the input values to use below 
$strings_to_test = array('aifkcwh', 'aifkcwhsz', 'kim', 'aif');  


 // get all possible combianations of $kantha+$talu+$murdha
$combinations = get_string_combinations(); 
display($combinations,'$combinations'); 

foreach ($strings_to_test as $stti => $string)  // 'aifkcwh', 'aifkcwhsz', 'kim', 'aif'
{

    $values = array($string);  
    reset($values); 

    display("<br/>-----------------------------------------"); 
    display("value: $string");  // first iteration "aifkcwh"

    // loop through $values using an array pointer 
    // while the current array pointer position is not null/false
    while(current($values)!==false)
    {
        // on the first iteration, $values will be the inputted string 
        // from $strings_to_test... our example is "aifkcwh"
        $value = current($values); 

       display("current value: $string"); 

        // for each possible combination of $kantha.$talu.$murdha
        // let's say our first combination is "aif"
        foreach($combinations as $ci => $combination)
        {  
            // look and see if the current value we are looking
            // at ("aifkcwh") contains this combination string  
            if (stripos($value,$combination)!==false)
            {
                // if it does... we perform the string mutation
                // "aif" does exist in the string "aifkcwh"

                // get the second letter in the combination string
                $single = $combination[1];   // i 

                // double that second letter
                $double = str_repeat($single, 2); // ii 

                // create a new string that is the combination with the 
                // second letter doubled... 
                $newcom = str_ireplace($single, $double, $combination); // aiif 

                // replace that string in $values so that
                // aifkcwh becomes aiifkcwh
                $newval = str_ireplace($combination, $newcom, $value); // aiifkcwh

                // does the new value ("aiifkcwh") exist in $values?  
                // have we already recorded this mutation?  
                if (!in_array($newval,$values)) 
                { 
                    // if not... append it to the array $values
                    $values[] = $newval; 
                    display("added to array for $value: $newval");

                    // now, values would go from being = array([0]=>'aifkcwh'); 
                    // to array ([ 0] => 'aifkcwh', [1] => 'aiifkcwh' ); 
                }
                else 
                {
                    display("not added because it already exists: $newval");
                }
            } 
        } // <-- end of the foreach statement, this will go through all combinations 
          //     in our combinations array for this particular value which is currently aifkcwh


        // next($values) increments the array pointer so that we move to the next
        // value in the $values array.  since we just added a value, 
        // $values now contains array ([ 0] => 'aifkcwh', [1] => 'aiifkcwh' ); 

        // before this statement index 0, current($values) == 'aifkcwh'
        next($values);  
        // after this statement index 1, current($values) == 'aiifkcwh'
        // for the next loop, we will test this string for all the combinations
        // if there is no next value, the `while` loop will end 
    }  

    // after we have gone through every possible combination for "aifkcwh",
    // we will have something like this: 
    /*
        Array
          (
                [0] => aifkcwh
                [1] => aiifkcwh
                [2] => aifkccwh
                [3] => aiifkccwh
          )
    */
    // and we add that to the $output array that contains an index for 
    // each input string, which contains all possible mutations of that string 
    $output[$string] = $values; 
}

 // display the results.  
display($output,'$output');  


 // ================================================================
 // functions 
 // ================================================================


 // get all possible combinations of $kantha.$talu.$murdha
 function get_string_combinations()
 {
     $kantha = array("a","k","g","n","h");
     $talu   = array("i","c","j","y","s");
     $murdha = array("f","w","q","r","z");

     $combinations = array(); 

     foreach($kantha as $k) // "a","k","g","n","h"
     { 
        foreach($talu as $t) // "i","c","j","y","s"
        { 
            foreach($murdha as $m) // "f","w","q","r","z"
            {
                $combinations[] = $k.$t.$m; 
            } 
        } 
     } 
     // this gives us an array if 125 items 
     /*
     $combinations = 
        Array
        (
             [0] => aif
             [1] => aiw
             [2] => aiq
             [3] => air
             [4] => aiz
             .... 
             [121] => hsw
             [122] => hsq
             [123] => hsr
             [124] => hsz
        ) 
     */
     return $combinations;
 }


 // display variables to see what the code is doing 
 function display($var,$label=null)
 {
     if (is_array($var))
     { 
        print "<pre>$label = \n".print_r($var,true)."</pre>"; 
     }
     else print "$var <br/>"; 
 }
?>

程序的完整输出除了$combinations因为它相当长......

----------------------------------------- 
value: aifkcwh 
current value: aifkcwh 
added to array for aifkcwh: aiifkcwh 
added to array for aifkcwh: aifkccwh 
current value: aifkcwh 
added to array for aiifkcwh: aiifkccwh 
current value: aifkcwh 
not added because it already exists: aiifkccwh 
current value: aifkcwh 

----------------------------------------- 
value: aifkcwhsz 
current value: aifkcwhsz 
added to array for aifkcwhsz: aiifkcwhsz 
added to array for aifkcwhsz: aifkccwhsz 
added to array for aifkcwhsz: aifkcwhssz 
current value: aifkcwhsz 
added to array for aiifkcwhsz: aiifkccwhsz 
added to array for aiifkcwhsz: aiifkcwhssz 
current value: aifkcwhsz 
not added because it already exists: aiifkccwhsz 
added to array for aifkccwhsz: aifkccwhssz 
current value: aifkcwhsz 
not added because it already exists: aiifkcwhssz 
not added because it already exists: aifkccwhssz 
current value: aifkcwhsz 
added to array for aiifkccwhsz: aiifkccwhssz 
current value: aifkcwhsz 
not added because it already exists: aiifkccwhssz 
current value: aifkcwhsz 
not added because it already exists: aiifkccwhssz 
current value: aifkcwhsz 

----------------------------------------- 
value: kim 
current value: kim 

----------------------------------------- 
value: aif 
current value: aif 
added to array for aif: aiif 
current value: aif 
$output = 
Array
(
    [aifkcwh] => Array
        (
            [0] => aifkcwh
            [1] => aiifkcwh
            [2] => aifkccwh
            [3] => aiifkccwh
        )

    [aifkcwhsz] => Array
        (
            [0] => aifkcwhsz
            [1] => aiifkcwhsz
            [2] => aifkccwhsz
            [3] => aifkcwhssz
            [4] => aiifkccwhsz
            [5] => aiifkcwhssz
            [6] => aifkccwhssz
            [7] => aiifkccwhssz
        )

    [kim] => Array
        (
            [0] => kim
        )

    [aif] => Array
        (
            [0] => aif
            [1] => aiif
        )

)
于 2013-10-30T14:30:33.367 回答