simonp Posted May 13, 2006 Share Posted May 13, 2006 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-015394Can anyone help!?!?I've been playing for a few hours but my knowledge of Perl isn't translating very well :)CheersSimon Link to comment https://forums.phpfreaks.com/topic/9599-solved-creating-string/ Share on other sites More sharing options...
wildteen88 Posted May 13, 2006 Share Posted May 13, 2006 Prettry simple to do:[code]<?php// create a function which will generate the string for usfunction 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 stringecho 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 https://forums.phpfreaks.com/topic/9599-solved-creating-string/#findComment-35470 Share on other sites More sharing options...
simonp Posted May 13, 2006 Author Share Posted May 13, 2006 Thanks wildteen88Had a play and got it working first time :)CheersSimon Link to comment https://forums.phpfreaks.com/topic/9599-solved-creating-string/#findComment-35478 Share on other sites More sharing options...
wildteen88 Posted May 13, 2006 Share Posted May 13, 2006 no problem! Link to comment https://forums.phpfreaks.com/topic/9599-solved-creating-string/#findComment-35484 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.