HoTDaWg Posted January 31, 2009 Share Posted January 31, 2009 hi there this script reads a bunch of words from a text file and adds them to an array. Ultimately it creates this string with either just a word or a number added to it or a letter added to it. The problem is everytime there is a word, it adds a space behind it? wats up with this... ive tried ereg_replace, str_replace, and ereg replace at the ultimate string even but it still doesnt work! any ideas? <?php error_reporting(E_ALL); $username = "hi29a"; $password = "hello"; #assign a letter to a key in an array<br> #split array or use { } to refer to a specific character within a string $stringofletters = "abcdefghijklmnopqrstuvwxyz"; $letters = str_split($stringofletters); #count how many lines there are in total of the username text document $linecount = 0; if ($file = fopen("usernames.txt","r")) { while(!feof($file)) { $line = fgets($file,5000); $linecount ++; } } #for each username, add that username to an array. $names = array(); if ($file = fopen("usernames.txt","rb")) { for($i = 1; $i <= $linecount; $i++) { $line = fgets($file,5000); $names [$i] = strtolower($line); } } else { echo "could not open file."; } #develop username, one represents just a word, two represents a word and a number, three represents a word, a number, and a letter at the end $selection = rand(1,3); switch($selection) { case 1: $random = rand(1,$linecount); $attempt = $names[$random]; break; case 2: $random = rand(1,$linecount); $attempt = $names[$random].rand(1,999); break; case 3: $random = rand(1,$linecount); $letter = rand(1,24); $attempt = "$names[$random]".rand(1,999)."$letters[$letter]"; break; } $finalattempt = str_replace(' ', '', $attempt); print $finalattempt; ?> thanks! Quote Link to comment Share on other sites More sharing options...
Prismatic Posted January 31, 2009 Share Posted January 31, 2009 Try this <?php error_reporting(E_ALL); $username = "hi29a"; $password = "hello"; #assign a letter to a key in an array<br> #split array or use { } to refer to a specific character within a string $stringofletters = "abcdefghijklmnopqrstuvwxyz"; $letters = str_split($stringofletters); #count how many lines there are in total of the username text document $linecount = 0; if ($file = fopen("usernames.txt","r")) { while(!feof($file)) { $line = fgets($file,5000); $linecount ++; } } #for each username, add that username to an array. $names = array(); if ($file = fopen("usernames.txt","rb")) { for($i = 1; $i <= $linecount; $i++) { $line = fgets($file,5000); $names[$i] = trim(strtolower($line)); } } else { echo "could not open file."; } #develop username, one represents just a word, two represents a word and a number, three represents a word, a number, and a letter at the end $selection = rand(1,3); switch($selection) { case 1: $random = rand(1,$linecount); $attempt = $names[$random]; break; case 2: $random = rand(1,$linecount); $attempt = $names[$random].rand(1,999); break; case 3: $random = rand(1,$linecount); $letter = rand(1,24); $attempt = $names[$random]. rand(1,999). $letters[$letter]; break; } //$finalattempt = str_replace(' ', '', $attempt); print $attempt; ?> Added a trim to the line where you assign the names. Quote Link to comment Share on other sites More sharing options...
HoTDaWg Posted January 31, 2009 Author Share Posted January 31, 2009 excellent thanks very much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.