문제

How can I assign random character before and after the numbers? it gives me same random character when I run the script.

Here's a example code:

<?php
function randomChar($length = 1) 
{
    $str = ""; 
    $characters = array_merge(range('A','Z'));
    $max = count($characters) - 1;
        for ($i = 0; $i < $length; $i++) 
        {   
            $rand = mt_rand(0, $max);
            $str .= $characters[$rand];
        }  
        return $str; 
}

$test = "0123456789";

echo implode(randomChar(), str_split($test, 1));
?>

when I run the script above it gives me the output of:

Output: 0Y1Y2Y3Y4Y5Y6Y7Y8Y9

My desired output must be:

Output: 0A1V2J3Y4Z5Q6S7O8R9

can someone help me to figure it out? thank you :)

도움이 되었습니까?

해결책

Currently, you only fetch a single random character and use it to join the characters together - this means that the same character will be used every time.

If you want to generate a new character for each delimiter, you will need to pass the input string into your function, something like this:

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");

Example output:

0L1E2G3T4F5S6E7F8I9M

Note that this code doesn't do precisely what you want, but it should give you a push in the right direction ;-)

다른 팁

When calling randomChar(), you need to specify the length of the random characters you want to generate. Otherwise, it will just create 1 random character (default values).

The implode() function you are using will actually paste the (array) values from second parameter and make the first parameter as the 'glue'. As such, Even if your randomChar() returns more than 1 character, the strings will be repeated for every numbers you want to combine.

Example: 0ABC2ABC ... 8ABC9

What you need to do instead is:

  1. Create an empty string to hold the resulting string.
  2. Generate randomChar() to be as many as the test string (or -1 if you only want the characters to be between numbers).
  3. For each of the numbers, concatenate the number and the random character and put it in the string from #1.
  4. Print the result.

Code:

function randomChar($length = 1)
{
    $str = "";
    $characters = array_merge(range('A','Z'));
    $max = count($characters) - 1;
        for ($i = 0; $i < $length; $i++)
        {
            $rand = mt_rand(0, $max);
            $str .= $characters[$rand];
        }
        return $str;
}

$test = "0123456789";

$length = strlen ($test);
$combined = '';
$random_char = randomChar ($length);

for ($i = 0;$i < $length; $i++)
{
   $combined .= $test[$i] . $random_char[$i];
}


print $combined . "\n";

Sample output: codepad

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top