A
A
AndRussia2021-07-24 16:08:54
PHP
AndRussia, 2021-07-24 16:08:54

Why is my generator not working?

Hello. I made a generator of random letters and numbers by mask.
There is a variable , which, respectively, is the same mask. The generator itself:$promo_mask = 'XXXX-5555-XXXX-5555';

$mask_count = substr_count($promo_mask, 'X');

  $chars = '12345ABCDEFGHIJKLMNOPQRSTUVWXYZ67890';
  $hashpromo = '';
  for($ichars = 1; $ichars <= $mask_count; ++$ichars) {
    	$random = str_shuffle($chars);
    	$promo = str_replace('X', $random[$ichars], $promo_mask);
  }
  echo $promo;

At the output I get something like this: DDDD-5555-DDDD-5555, although the desired result looks like this: A6JS-5555-0ORP-5555
The same letter is assigned to each x. I guess that the design itself is not correct, but I can’t figure out what. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2021-07-24
@AndRussia

str_replace - Replaces all occurrences of the search string with the replacement string,
easiest to do like this. take the current character
along the entire length of the mask . if it is X then add a random symbol to the result, otherwise the current one

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question