Jump to content


Photo

*SOLVED* Creating string


  • Please log in to reply
3 replies to this topic

#1 simonp

simonp
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 13 May 2006 - 12:42 PM

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

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 13 May 2006 - 01:12 PM

Prettry simple to do:
<?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');

?>
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.

#3 simonp

simonp
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 13 May 2006 - 01:57 PM

Thanks wildteen88

Had a play and got it working first time :)

Cheers

Simon

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 13 May 2006 - 02:14 PM

no problem!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users