Word generator based on alphabet

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dr.Brainiac
Forum Newbie
Posts: 4
Joined: Fri Oct 01, 2004 4:20 am

Word generator based on alphabet

Post by Dr.Brainiac »

Hi all,


Im stuck with something. Im trying to make a generator. It will grab info from dns whois every 12 seconds and put it in my mysql database. Herefore i want to generate possible domains without extension (only for .be domains) based on the alphabet.

I have a page that reloads itself every 12 seconds (javascript). Using session variables i should be able to generate a new domainname each time the page reloads.

This far i get, but i have no idea how to generate these words. I started with something simple but im stuck:

Code: Select all

$tekens = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","-");
if($counter < count($tekens)){
	$domain = "";
	for($i=0;$i<$aantal;$i++){
		$domain .= $tekens[$counter];
	}
	print($domain);
	$counter++;
	//do the actual whois lookup and put it to db
}else{
	$counter = 0;
	$aantal++;
}


where $aantal would be how many times i looped the whole array of $tekens, and $counter would be a counter to loop the array $tekens. These two vars are provided by sessions.


Any idea's to get me started here?

thx a lot
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]mt_rand[/php_man]() / [php_man]array_rand[/php_man]()
Dr.Brainiac
Forum Newbie
Posts: 4
Joined: Fri Oct 01, 2004 4:20 am

Post by Dr.Brainiac »

Thx for the tip.

I accomplished to get it working with mt_rand. The only problem is that it will take a long long time before i get decent results by using a random factor.

Isn't there another way to generate these domainnames not random?

like

a
aa
aaa
ab
abaa
...

I just need to find how to get all possible words by using just the letters of the alphabet, numbers and "-".

Oh and the "-" char cannot be on the first 3 characters of the domainname.

All other suggestions welcome ;)
d_d
Forum Commoner
Posts: 33
Joined: Wed Jul 07, 2004 4:56 pm
Location: UK

Post by d_d »

It's still going to take a long time if you do it like that. Whats the largest name you are going to test for?
To do up to 6 letter strings there will be 402,321,221 combinations. Do one every 12 seconds and it will take 55,877 days (152 years!) to complete.
(Thats if my maths is correct!)

If you want to do it for a sane lenght string can't you just use a series of for loops?

Code: Select all

<?php
$tekens = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","-");
for ($a=0;$a<count($tekens);$a++)
{
	$domain = $tekens[$a];
	//test domain
}
for ($a=0;$a<count($tekens);$a++)
{
	for ($b=0;$b<count($tekens);$b++)
	{
		$domain = $tekens[$a].$tekens[$b];
		//test domain
	}
}
for ($a=0;$a<count($tekens);$a++)
{
	for ($b=0;$b<count($tekens);$b++)
	{
		for ($c=0;$c<count($tekens);$c++)
		{
			$domain = $tekens[$a] . $tekens[$b] . $tekens[$c];
			//test domain
		}
	}
}
?>
Not very elegant but works. You proberly want to cut out the obvious bad ones, where there are two '-' or it starts or ends with '-'.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

here's a stripped down version of something similar I wrote recently for something else. :)

Code: Select all

&lt;?php

	function nextAttempt($characters, $current = '')
	{
		$current = (string)$current;
		$chars = strlen($characters);
		$len = strlen($current);

		if($len === 0)
		{
			$text = chr($min);
		}
		else
		{
			$rolls = 0;
			$text = $current;
			$lastchar = $characters&#1111;$chars - 1];
			$firstchar = $characters&#1111;0];

			for($x = $len - 1; $x &gt;= 0; $x--)
			{
				if($text&#1111;$x] === $lastchar)
				{
					$rolls++;
					$text&#1111;$x] = $firstchar;
				}
				elseif($chr !== false)
				{
					$chr = strpos($characters,$text&#1111;$x]);
					$text&#1111;$x] = $characters&#1111;$chr + 1];
					break;
				}
				else
				{
					die('unknown character in stream');
				}
			}
			
			if($rolls == $len)
				$text .= $firstchar;
		}

		return $text;
	}
	
	$str = 'abcdefghijklmnopqrstuvwxyz0123456789-';
	
	$max_length = 20;
	$test = $_GET&#1111;'last'];

	if(strlen($working = nextAttempt( $str, $test )) &gt; $max_length)
	{
		//	stop working
	}
	else
	{
		//	continue working
	}

?&gt;
Dr.Brainiac
Forum Newbie
Posts: 4
Joined: Fri Oct 01, 2004 4:20 am

Post by Dr.Brainiac »

d_d wrote:It's still going to take a long time if you do it like that. Whats the largest name you are going to test for?
To do up to 6 letter strings there will be 402,321,221 combinations. Do one every 12 seconds and it will take 55,877 days (152 years!) to complete.
(Thats if my maths is correct!)
Thx a lot for the help guys. I will try this tomorrow, see what it does.

About the 152 years: it does not work like that actually, i had to give a little more information i guess. I will check a few domainnames every second actually with another server. If its a registered one, i will get the whois information and save it. I can only do that every 12 seconds, otherwise i will be banned from the whois.dns.be server. But the DAS server (check if its registered) doesnt use any bans, neither give the whois information.

So if i have a lot of registered domainnames in a row, yes it will take quite some time to get the information. but i guess the system will work fast this way.

Ah well, i am prepared to wait for a long time for that information, its worth it ;)
Dr.Brainiac
Forum Newbie
Posts: 4
Joined: Fri Oct 01, 2004 4:20 am

Post by Dr.Brainiac »

feyd wrote:here's a stripped down version of something similar I wrote recently for something else. :)
From what i see this could work, looks just what i need :lol:

Just using some loops will be difficult i think, because the page refreshes itself and i need to continue from the last generation, and not start all over every refresh. No idea how something like that could be done with loops ( :?: ).

I also read something about generating easy to remember passwords based on a list of words. markov chains or something like that :s Its not what im looking for but there might be a way to generate 'most likely to be a domainname' names, I still have to look more into this.

Im still brainstorming for other solutions, but didnt come up with anything else at the moment.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Just using some loops will be difficult i think, because the page refreshes itself and i need to continue from the last generation, and not start all over every refresh. No idea how something like that could be done with loops
you can have the initial value come in from the url, and the current value (after each pass) go into your refresh url.. if you use my code.
Post Reply