Jump to content

[SOLVED] Random space in string:S


HoTDaWg

Recommended Posts

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!

Link to comment
Share on other sites

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.

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.