Jump to content

Help with srand or shuffle and two txt files :(


aetheryte

Recommended Posts

Hello!

I've been coding PHP for approximately one month now with a friend of mine and we're enjoying it so far. I'm able to read code relatively well if it's in front of me and understand what is truly happening. However, I'm still unsteady on my own too feet just yet for writing my own code hehe :'(

 

I will explain what I am trying to accomplish. I'm attempting to build a php script for generating domain names using two txt files as you will see in my code. I've accomplished retrieving a random line from both files and concate them together. In addition to a very crude way to check if that generated name is available by whois (I have omitted this from the code haha).

 

The problem for me lies in more than one at a time without it looking terrible and I am confident there must be a better way.

 

I'm able to display more than one random domain name at a time although it's definitely inefficient. You will see from my code at how I am able to achieve that, yet when I want to make 20 it sure is ugly. I want to be able to echo or print X instances of a random domain name, properly. I've searched around for a couple of days attempting to pick up anything new however I'm still at a loss.

 

Any help with this would be greatly appreciated and I certainly won't forget it in the future. :)

 

The first working script with the ability to create new random domain names proceeding each other. I am aware of nl2br but not so sure how to implement it properly. I can do it and make it work with the $whois I have set up although it's not pretty at all ^^;

<?

// set prefix file and shuffle
$prefix = file("prefix.txt");
shuffle($prefix);

// set suffix file and shuffle
$suffix = file("suffix.txt");
shuffle($suffix);

// adding whois hyperlink
$whois = "http://www.networksolutions.com/whois-search/";

?>

<html>
<head>
<title> random name generation </title>
</head>

<body>

<center>
generating domain names
<br />


<?
    echo trim("$prefix[0]") . trim(" ") . trim("$suffix[0]") . ".com";
    echo "<br />";
    echo trim("$prefix[1]") . trim(" ") . trim("$suffix[1]") . ".com";
    echo "<br />";
    echo trim("$prefix[2]") . trim(" ") . trim("$suffix[2]") . ".com";
?>

</center>

</body>
</html>

 

Here is my second attempt with another script...

<?
        
$prefixline = 10;

$prefix = file("prefix.txt");

// checking the amount of lines in prefix text
$prelist = sizeof($prefix);

// srand generation
list($plsec,$psec) = explode(' ', microtime());
srand((float) $psec + ((float) $plsec * 1000000));

// prefix counter
$pcount = 0;

while ($pcount++ < $prefixline) {
    // random prefix id
    $preID = rand(0, $prelist-1);
    $prefix[$preID];
}



$suffixline = 10;

$suffix = file("suffix.txt");

// checking the amount of lines in suffix text
$suflist = sizeof($suffix);

// srand generation
list($slsec,$ssec) = explode(' ', microtime());
srand((float) $ssec + ((float) $slsec * 1000000));

// suffix counter
$scount = 0;

while ($scount++ < $suffixline) {
    // random suffix id
    $sufID = rand(0, $suflist-1);
    $suffix[$sufID];
}


$domain = trim("$prefix[$preID]") . trim(" ") . trim("$suffix[$sufID]");

?>

<html>
<head>
<title> random name generation </title>
</head>

<body>

<center>
<br />
new attempt for domain generation
<br />
<?

echo $domain;

?>

</center>
</body>
</html>

 

This second script does virtually the same thing. I could make a function for prefix and suffix (suffix 10 times below) however when it comes to concate'ing the two it's an issue. An example;

 

function showSuffix($suffixline = 10) {

all the suffix coding in here

}

For the html;
<?

showSuffix(10);

?>

 

This would merely output 10 new suffix but adding it together with 10 prefix before it is something I can't yet accomplish.

 

I am well aware my variable naming is a bit crude although I will definitely polish it up with whichever method is best. I truly appreciate anyone's help and I'm open to all critique/advice in order to become a better programmer down the road and hopefully help others in the future. :)

Link to comment
Share on other sites

Why not use array_rand instead of shuffle? .. Anyway, Your second script seems to list the domain:

$domain = trim("$prefix[$preID]") . trim(" ") . trim("$suffix[$sufID]");

Can you not just run that through a FOR loop and list a random domain 10 times? (prefix and all)

 

There's not a need for a function like 'showSuffix()' as it doesn't append the prefix, it'll just get in the way.

Link to comment
Share on other sites

Hi oni-kun, thanks for your response~

 

I have indeed tried to set up a loop. Although I really must be doing it wrong then. When I had set it up, it would loop $domain but it would be the same generation X times from what I set up in the loop. In other words, it didn't make the variable $domain... "variable" if that makes sense. My familiarity with for and foreach is pretty basic :/

Link to comment
Share on other sites

Hi oni-kun, thanks for your response~

 

I have indeed tried to set up a loop. Although I really must be doing it wrong then. When I had set it up, it would loop $domain but it would be the same generation X times from what I set up in the loop. In other words, it didn't make the variable $domain... "variable" if that makes sense. My familiarity with for and foreach is pretty basic :/

 

What you're doing is looping with the same generated variable. You must encorporate the random generation within the loop, so it is truly random each time, you can achieve this by placing your 'while' loops that calculate the random pre/suffixes into functions, and call them in the loop.

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.