如何在字符串中混音辅音?

时间:2018-02-11 09:25:32

标签: php shuffle

在PHP中,我们知道我们可以使用函数str_shuffle()随机调整字符串字符。因此,字符串"developer"每次都会变为"lrevdeope", "dvolpeere"等等。

但这不是我想要的。相反,我想随机改组辅音。因此,"developer"应在每次刷新页面时变为"verelodep", "leveroped"等。

我们怎样才能实现它?有什么想法吗?

4 个答案:

答案 0 :(得分:3)

几分钟后,我有了这个:

$s = 'developer';
$cons = [];
for ($i = 0; $i < strlen($s); $i++) {
    if (!in_array($s[$i], ['a', 'e', 'i', 'o', 'u', 'y'])) {
        $cons[] = $s[$i];
        $s[$i] = '-';
    }
}

shuffle($cons);

for ($i = 0; $i < strlen($s); $i++) {
    if ($s[$i] == '-') {
        $s[$i] = array_shift($cons);
    }
}

echo $s . PHP_EOL;

答案 1 :(得分:0)

您可以像这样创建自定义函数:

function shuffle_consonants($str) {
  $str = str_split($str);
  $vowels = ['a','e','i','o','u'];

  foreach($str as $char) 
      if(!in_array($char, $vowels)) $con .= $char;

  $con = str_shuffle($con);
  $idx = 0;

  foreach($str as &$char) 
      if(!in_array($char, $vowels)) $char = $con[$idx++];

  return implode("", $str);
}
echo shuffle_consonants("developer");

答案 2 :(得分:0)

这里有一个替代方案:

$word = "developer";

$letters = str_split($word);

$consonants = array_filter($letters, function ($letter) {
    return !in_array($letter, ['a', 'e', 'i', 'o', 'u', 'y']);
}); //Get all consonants with their index

$consonantPositions = array_keys($consonants); //keep indexes (shuffle will lose them)

shuffle($consonants);

$letters = array_combine($consonantPositions, $consonants) + $letters; // put the shuffled consonants in their indexes and glue them back into the letters

ksort($letters); // put the keys back in their place

echo implode("",$letters); 

答案 3 :(得分:0)

我很欣赏u_mulder方法的总体设计,但我想为可能感兴趣的读者做一些改进/微观优化。

  • 存储strlen()值,以便php不必重复生成它。
  • 致电strpos()以区分元音与辅音。 (参考:in_array vs strpos for performance in php

  • 不要使用-临时替换输入字符串中的辅音。

  • 只迭代第二个循环中的辅音(而不是迭代字符串中的所有字母)。
  • 在没有函数调用的情况下对字符串进行替换。

代码:(Demo

$string="developer";
$consonants=[];
$length=strlen($string);
for($offset=0; $offset<$length; ++$offset){  // iterate each letter of the string           ... OR for($offset=strlen($string); --$offset;){
    if(strpos('aeiou',$string[$offset])===false){  // isolate the consonants
        $consonants[]=$string[$offset];  // store the consonant
        $offsets[]=$offset;  // store the offset (aka indexed position of the consonant in the string)
    }
}
shuffle($consonants);  // shuffle the array of stored consonants

foreach($consonants as $index=>$consonant){  // iterate ONLY the stored consonants
    $string[$offsets[$index]]=$consonant;  // reassign the consonants in their new positions
}

echo $string;  // possible output: revepoled

这里有一个数组函数和foreach循环的混合,可以重新插入混洗的辅音:

$string="developer";
$consonants=array_diff(str_split($string),['a', 'e', 'i', 'o', 'u']);  // isolate consonants, preserve offsets as keys
$offsets=array_keys($consonants); // store copy of offsets before shuffling
shuffle($consonants);  // shuffle the array of stored consonants (returned value is re-indexed)

foreach($consonants as $i=>$consonant){
    $string[$offsets[$i]]=$consonant;  // reassign the shuffled consonants at the known consonant positions
}

echo $string;

对于任何认为我没有提供任何独立想法的人......这是另一种方法,它将实现两个字符串函数调用,然后是正则表达式函数调用(这将对速度产生负面影响,但不会非常可怕)这可能写成一个双线。

代码:(Demo

$word="developer";

$shuffled_consonants=str_shuffle(str_replace(['a','e','i','o','u'],'',$word));  // generate shuffled string of consonants

// reinsert shuffled consonants at original consonant positions
echo preg_replace_callback(
    '~[^aeiou]~',                                 // match each consonant at original position
    function($m)use($shuffled_consonants){        // pass in the shuffled string
        static $offset=0;                         // init the offset counter
        return $shuffled_consonants[$offset++];   // insert new consonant at original position using post-incrementation
    },
    $word);