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
https://forums.phpfreaks.com/topic/143217-solved-random-space-in-strings/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.