Jump to content

Archived

This topic is now archived and is closed to further replies.

simonp

*SOLVED* Creating string

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Thanks wildteen88

Had a play and got it working first time :)

Cheers

Simon

Share this post


Link to post
Share on other sites

×

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.