如何在PHP中生成随机字符串(使用3个字符串和随机数)

Kil*_*ish 0 php string random

我有三根弦,我想生成一个随机字符串使用这些字符串3包括一些随机数串之间.

谢谢

Ex: first string : john
    second string : smith
    third string : john9k

    I want a random string like : john.simth190, smith.john9k, john9k.123.smith, etc.,
Run Code Online (Sandbox Code Playgroud)

如何在PHP中执行此操作.

谢谢

Jas*_*son 5

你可以尝试这样的事情:

<?php 
function random($items, $min=0, $max=100, $random_parts=2, $delimiter="."){
    #make sure items is an array
    $items = (array)$items;        

    #add as many random bits as required.
    for($x=0; $x<$random_parts; $x++)
          $items[] = rand($min, $max);

    #shuffle and join them
    shuffle($items);
    return implode($delimiter, $items);
}
Run Code Online (Sandbox Code Playgroud)

基本上它所做的就是接受一个名字数组,数组('john','smith','john9k').然后它需要min rand和max rand参数.最后它接受你想要的随机数量.

所以打电话给我我会这样做:

<?php
echo random(array('john','smith','john9k'), 0, 100, rand(0,10));
Run Code Online (Sandbox Code Playgroud)