0

I have string $template= "KEK-####"
I need to replace all '#' symbols with random int and return modified string
I tried this:

$result = '';

foreach (str_split($template) as $templateChar){
  if ($templateChar === '#'){
    $templateChar = random_int(1, 9);
  }
  $result .= $templateChar;
}
return $result;

And it works but it fails when there's a lot of new strings to generate (too many foreach loops with if else condition in them)

$template = str_replace('#', random_int(1,9), $string); doesn't work for me because it will return "KEK-888888" and I need my new strings to be unique Is there a way to get rid of this foreach loop or make it faster?

0 Answers0