I am looking for away to Generate Name a from Text Document on Server. Example would be:
If I save a text document in the server like at www.example.com/txt/names.txt

How would I be able to generate 1 name at random?

Recommended Answers

All 15 Replies

Use logic and imagination. To create a name, you must analyze what the letters, often found in names. Also, bundles of vowels and consonants.

Use logic and imagination. To create a name, you must analyze what the letters, often found in names. Also, bundles of vowels and consonants.

Oh forgot to say. I am new to this too

-> You can read your text file and from text file's content you can take first three or four words and use it as file name.

-> or use php random function to create random name.

function getRandomAlphaNumeric($intpLength = 16)
{
	$arrAlphaNumeric = array();
	$arrAlpha = range('A','Z');
	$arrNumeric = range(0,9);
	
	$arrAlphaNumeric = array_merge($arrAlphaNumeric, $arrAlpha, $arrNumeric);
	
	mt_srand((double)microtime() * 1234567);
	shuffle($arrAlphaNumeric);

	$strAlphaNumeric = '';
	for($x=0; $x<$intpLength; $x++)	
		$strAlphaNumeric .= $arrAlphaNumeric[mt_rand(0, (sizeof($arrAlphaNumeric)-1))];
	return $strAlphaNumeric;	
}
$strAlphaNumeric = '';

So is there would I put the source?

No changes in function.
you can use this function as:
$filename = getRandomAlphaNumeric(10);
So that you can have one random number in $filename.

what exactly ur scenario is??
Do you have txt files already on server or you want to upload txt file and rename it??

I have the txt file on my server and I do want to rename it when needed.

<?php
$sourcepath = "C:/my/path/";
$oldfilename = "myfile.txt";
$newfilename = getRandomAlphaNumeric(10);
rename($sourcepath.$oldfilename, $sourcepath.$newfilename );
echo "file ".$oldfilename." is now renamed to ".$newfilename;
?>

Hope this will help.

<?php
$sourcepath = "http://www.example.com/myfile.txt";
$oldfilename = "myfile.txt";
$newfilename = getRandomAlphaNumeric(10);
rename($sourcepath.$oldfilename, $sourcepath.$newfilename );
echo "file ".$oldfilename." is now renamed to ".$newfilename;
?>
<?php
$filename = getRandomAlphaNumeric(10);($intpLength = 16)

[B]	$arrAlphaNumeric = array();[/B]
	$arrAlpha = range('A','Z');
	$arrNumeric = range(0,9);
	
	$arrAlphaNumeric = array_merge($arrAlphaNumeric, $arrAlpha, $arrNumeric);
	
	mt_srand((double)microtime() * 1234567);
	shuffle($arrAlphaNumeric);

	$strAlphaNumeric = '';
	for($x=0; $x<$intpLength; $x++)	
		$strAlphaNumeric .= $arrAlphaNumeric[mt_rand(0, (sizeof($arrAlphaNumeric)-1))];
	return $strAlphaNumeric;	
}
?>

Ok I added the code as you see and I get this error on line 11 (one bolded above:
Parse error: syntax error, unexpected T_VARIABLE

I think you don't know how to use function.

<?php

function getRandomAlphaNumeric($intpLength = 16)
{
	$arrAlphaNumeric = array();
	$arrAlpha = range('A','Z');
	$arrNumeric = range(0,9);
	
	$arrAlphaNumeric = array_merge($arrAlphaNumeric, $arrAlpha, $arrNumeric);
	
	mt_srand((double)microtime() * 1234567);
	shuffle($arrAlphaNumeric);

	$strAlphaNumeric = '';
	for($x=0; $x<$intpLength; $x++)	
		$strAlphaNumeric .= $arrAlphaNumeric[mt_rand(0, (sizeof($arrAlphaNumeric)-1))];
	return $strAlphaNumeric;	
}

$sourcepath = "C:/my/path/"; // this must be local pc path not LIVE URL
$oldfilename = "myfile.txt";
$newfilename = getRandomAlphaNumeric(10);
rename($sourcepath.$oldfilename, $sourcepath.$newfilename );
echo "file ".$oldfilename." is now renamed to ".$newfilename;
?>

Find this link, you can see php function example here.

And you want to rename your file which is already there on your server .. right?
So use path not url.

ITs renaming the file? Is it suppose to, because I wanted it to generate a name from the text file.

php code for you:

<?php
		
	$content = file_get_contents("http://192.168.0.1/domain/test.txt");	
	$content = ereg_replace("[\n\r]", "##", $content);
	$ar = explode('####',$content);
	$rand_keys = array_rand($ar, 1);
	$random = $ar[$rand_keys];
	echo $random;
	
?>

test.txt :

Computer
World
Hello
Monday
File
Mouse
Tree

Take care that all words in test.txt must be in next line.

It's weird. It puts them all of them like this:

Computer##World##Hello##Monday##File##Mouse##Tree

Can I just get one of them at random?

it was working at my end.. try this.

<?php
		
	$content = file_get_contents("http://192.168.0.1/domain/test.txt");	
	$content = ereg_replace("[\n\r]", "##", $content);
	$ar = explode('##',$content);
	$rand_keys = array_rand($ar, 1);
	$random = $ar[$rand_keys];
	echo $random;
	
?>

Thanks that really helped. Your awesome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.