- preg_replace_callback: documentation ( source)
- str_shuffle: documentation ( source)
- str_replace: documentation ( source)
<?php
$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);