Jump to content

*SOLVED* Creating string


simonp

Recommended Posts

Hi,

I need to be able to create a a string with the following information:

a standard word followed by the date in digits followed by a random 6 digit number - all separated by hypens, eg:

word-20060513-015394

Can anyone help!?!?

I've been playing for a few hours but my knowledge of Perl isn't translating very well :)

Cheers

Simon
Link to comment
Share on other sites

Prettry simple to do:
[code]<?php

// create a function which will generate the string for us
function createString($word="word")
{
    // setup the date in this format yyyymmdd
    $date = date("Ymd");

    // setup an empty rand_str variable for use later
    $rand_str = "";

    //list all the possible characters that can be in our 6 letter random string
    $str = "abcdefghijklmnopqrstuvwxyz";

    //creating our 6 lettered random string
    for($i = 0; $i < 6; $i++)
    {
        //randomly choose a character from the str variable
        $rand_str .= substr($str, rand(0, strlen($str)-1), 1);
    }

    // putting it alltogether
    $string = $word . '-' . $date . '-' . $rand_str;

    // send back our generated string!
    return $string;
}

//call createString function with out defining a string
echo createString();

echo "<br />";

//call createString function again with a word defined!
echo createString('apples');

?>[/code]
The code has comments in so you can understand what is going on. Have a little play with it to understand what is going on.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.