1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help: PHP script - generate random string + text + multiple lines

Discussion in 'PHP' started by n00bl3t, May 10, 2016.

  1. #1
    got this script... but it is not quite what I needed... that is why I am seeking help...

    This is the code:
    <?php
    function generateRandomString($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
    
    // Echo the random string.
    // Optionally, you can give it a desired string length.
    <?php echo generateRandomString(); ?>
    ?>
    PHP:
    basically, I wanted this small php script to generate thousands of lines (~3000)...

    in this format:

    mytext/(the_random_string_here)
    mytext/(the_random_string_here)
    mytext/(the_random_string_here)
    mytext/(the_random_string_here)
    mytext/(the_random_string_here)
    up to the very end (~3000 lines)
    with each line have a random string...

    Thank you so much guys!
     
    n00bl3t, May 10, 2016 IP
  2. Rahul Vaishnav

    Rahul Vaishnav Active Member

    Messages:
    123
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    63
    #2
    I am interested but can you tell me how to add this "mytext" ?
     
    Rahul Vaishnav, May 10, 2016 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #3
    I think you've missed the point.

    @n00bl3t you just need to put a loop in there to run it the 3000 times. I've assumed you are trying to create some sort of link and I've put them into an unordered list. Use what you need but it should give you something to work with.

    
    function generateRandomString($length=10){
    // everything in your existing function looks fine
    }
    
    for ($i = 0; $i < 3000; $i++){
       $str = generateRandomString();
       echo "<li><a href='/mytext/{$str}'>{$str}</a></li>";
    }
    
    Code (markup):
     
    sarahk, May 11, 2016 IP
    Vooler and KangBroke like this.
  4. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #4
    Not to interrupt much, but here is your function's a little faster version:
    
    <?php
    
      function generateRandomString($length = 10)
      {
          srand(time());
          $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
          if ($length > strlen($characters))
              $length = strlen($characters);
          $characters = str_shuffle($characters);
          return substr($characters,0,$length);
      }
    
    Code (markup):
    Stay well...
     
    Vooler, May 13, 2016 IP
    sarahk likes this.
  5. sajjadhira

    sajjadhira Greenhorn

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #5
    You can user this function for generate random string,
    function rand_code($len)
    {
    $min_lenght= 0;
    $max_lenght = 100;
    $bigL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $smallL = "abcdefghijklmnopqrstuvwxyz";
    $number = "0123456789";
    $bigB = str_shuffle($bigL);
    $smallS = str_shuffle($smallL);
    $numberS = str_shuffle($number);
    $subA = substr($bigB,0,5);
    $subB = substr($bigB,6,5);
    $subC = substr($bigB,10,5);
    $subD = substr($smallS,0,5);
    $subE = substr($smallS,6,5);
    $subF = substr($smallS,10,5);
    $subG = substr($numberS,0,5);
    $subH = substr($numberS,6,5);
    $subI = substr($numberS,10,5);
    $RandCode1 = str_shuffle($subA.$subD.$subB.$subF.$subC.$subE);
    $RandCode2 = str_shuffle($RandCode1);
    $RandCode = $RandCode1.$RandCode2;
    if ($len>$min_lenght && $len<$max_lenght)
    {
    $CodeEX = substr($RandCode,0,$len);
    }
    else
    {
    $CodeEX = $RandCode;
    }
    return $CodeEX;
    }
    echo rand_code(10);
    
    PHP:
     
    sajjadhira, May 27, 2016 IP
  6. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #6
    One more version accommodating longest possible strings
    
      /**
      * accommodates long string creation
      */
      function generate_random_string($length = 10)
      {
          $buf = implode('', array_merge(range('0','9'), range('a','z'), range('A','Z')));
          if ($length > strlen($buf))
              $buf = str_repeat($buf, ceil($length / strlen($buf)));
      
          return str_shuffle(substr($buf, 0, $length));
      }
    
       #Try longer string
       print generate_random_string(1024);
    
    PHP:
     
    Vooler, May 28, 2016 IP