Simple PHP Help

bluemagic

Regular Member
Joined
Apr 22, 2008
Messages
273
Reaction score
40
Hey guys,

I need a simple php code that creates a list of two characters from the alphabet at random, like:

jy
gu
yd
qb
ik
jp

etc, to how many times I want.

It doesn't need a GUI. I can edit the part of the code to change the number of results I need.
 
Winner gets 100 autopligg links (the ones that work) to anywhere they want!
 
Hi,
Hope this helps you.

Code:
<?php 
function rand_chars($c, $l, $u = FALSE) { 
 if (!$u) for ($s = '', $i = 0, $z = strlen($c)-1; $i < $l; $x = rand(0,$z), $s .= $c{$x}, $i++); 
 else for ($i = 0, $z = strlen($c)-1, $s = $c{rand(0,$z)}, $i = 1; $i != $l; $x = rand(0,$z), $s .= $c{$x}, $s = ($s{$i} == $s{$i-1} ? substr($s,0,-1) : $s), $i=strlen($s)); 
 return $s; 
} 
?> 
 
string $c is the string of characters to use. 
integer $l is how long you want the string to be. 
boolean $u is whether or not a character can appear beside itself. 
 
Examples: 
rand_chars("ABCEDFG", 2);
rand_chars("ABCEDFG", 2, TRUE);
 
Thanks. Please give me more instructions on how to edit this. How can I customize how many of these I want?
 
Lets say one day I need 10000 and another I need 1000000.
 
Here's what you need (edited the posted script, so some credit to catchout)
PHP:
<?php
function rand_chars($c, $l, $u = FALSE) {
     if (!$u) for ($s = '', $i = 0, $z = strlen($c)-1; $i < $l; $x = rand(0,$z), $s .= $c{$x}, $i++);
     else for ($i = 0, $z = strlen($c)-1, $s = $c{rand(0,$z)}, $i = 1; $i != $l; $x = rand(0,$z), $s .= $c{$x}, $s = ($s{$i} == $s{$i-1} ? substr($s,0,-1) : $s), $i=strlen($s));
     return $s;
}

function print_list($num, $seperator)
{
    $I = 0;
    for ($I = 0; $I < $num; $I++)
    {
        echo rand_chars("abcdefghijklmnopqrstuvwxyz", 2).$seperator;
    }

}

    print_list(100,"<br />");
?>

Just run print_list with w/e number you want and whatever separator you want and it will print them. (An example is shown at the bottom.)
 
Okay

Both of you get 1000 links each for free to unlimited sites.

Please PM me your URLs!
 
It didnt work.

Parse error: syntax error, unexpected T_LNUMBER, expecting '&' or T_VARIABLE in /home/site/public_html/script.php on line 8
 
It didnt work.

Parse error: syntax error, unexpected T_LNUMBER, expecting '&' or T_VARIABLE in /home/site/public_html/script.php on line 8

Did you by chance mis-copy the code? I tested the snippet drkenneth provided and it worked fine.
 
okay I made a careless mistake.

People send me your urls!
 
I also need this function not to repeat!!
 
What do you mean? You want just 1 at a time or a list of a bunch?
 
I dont want a result repeated.

I generated 10000

and sg was repeated more than 5 times.
 
oh okay.

Anyone that contributed here, send me a PM and i'll run 1000 links for you!
 
here's another way...

PHP:
<?php

$how_many_times = 10;
$group_size = 2;
$letters = str_split("abcdefghijklmnopqrstuvwxyz");

while($i++ <= $how_many_times) {
	shuffle($letters);
	print implode("", array_shift(array_chunk($letters, $group_size))) . "<br>";
}

?>

$how_many_times is how many times you want to print the characters out...
$group_size is how many letters in the group...your request was 2...
$letters is the alphabet, but you could add any other text, punctuation, etc in there...

This one basically stacks commands to keep the code short, but it loops 'x' number of times, shuffles (randomizes) the list of letters each time, then breaks it into groups of whatever size you specify, and spits out one of those random groups...it's fast and simple.

Note...for anyone curious, I use array_shift instead of array_pop, because pop takes from the right end, and without knowing the size of the letters array and group_size beforehand, we could end up with odd numbers in chunking, so it's better to take from the left...

anyway, any questions hit me up...
 
If anybody of U have some more specific codes, expect those which are enlisted here, do give me a PM. I m interested in getting those.
 
Back
Top