PHP可选择替换字符

时间:2013-10-30 13:17:54

标签: php replace preg-replace character str-replace

我正在使用PHP来做一些梵语编码。 我将举例说明该规则: 规则是当$ kantha- $ talu- $ murdha以相同的顺序出现时,$ talu可选择重复。 例如aif - > aiif。 如果只有一个这样的事件,下面的脚本给我输出。 但是如果字符串中出现多个这样的事件,则脚本会失败。例如$ 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>";
?>

1 个答案:

答案 0 :(得分: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
        )

)