- array_push: documentation ( source)
- mt_rand: documentation ( source)
- implode: documentation ( source)
- str_split: documentation ( source)
<?php
function insert_random_chars($str, $alphabet = "ABCDEFGHIJKLMNOPGRSTUVWXYZ")
{
// An array to hold the result
$result = array();
// The highest character index in the string
$max = strlen($alphabet) - 1;
// Loop the characters in the input string
foreach (str_split($str, 1) as $char) {
// Add the current character and a random character to the output array
array_push($result, $char, $alphabet[mt_rand(0, $max)]);
}
// Join the output array together
return implode('', $result);
}
echo insert_random_chars("0123456789");