Jump to content

Recommended Posts

Hey PHPFreaks. I am a new guy. I need the assistance your superpowers please?

 

Here's the idea.. A user inputs a name into a form, which then gets posted.

 

They then get a random sentence returned, containing the name they posted.

 

 

 

To do this, I want to use a text file containing the sentences. (Note: the variable appears in different places)

 

Eg: (example.txt)

 

One day I went to the park with $name

One day $name went to the park with me

I saw $name at the park one day

$name and I were at the park

 

 

So, what would the code be to echo a single line at random and the variable $name to be recognized in the echo ?

 

Thanks.  :D

Perhaps something like:

 

$str = 'One day I went to the park with {name}
One day {name} went to the park with me
I saw {name} at the park one day
{name} and I were at the park';

$str = str_replace('{name}', $name, $str);

In your text file write:

 

One day I went to the park with %s

One day %s went to the park with me

I saw %s at the park one day

%s and I were at the park

 

In your php file write:

 

$sentences = file('path/to/text/file');
$rand = array_rand($sentences);
printf($sentences[$rand], $name);

 

In your text file write:

 

One day I went to the park with %s

One day %s went to the park with me

I saw %s at the park one day

%s and I were at the park

 

In your php file write:

 

$sentences = file('path/to/text/file');
$rand = array_rand($sentences);
printf($sentences[$rand], $name);

 

 

That's great! The variable gets echoed!

 

The only problem is, it is echoing all the lines at the same time, instead of plucking out just 1 at random

I got it!!

 

Here's how I did it.

 

{ 

for($i=9; $i>=0; $i=$i-1)

	{

	$linefile = "phptest.txt";
	$handle = fopen($linefile, "r");
	$contents = fread($handle, filesize($linefile));
	fclose($handle);
	$word1 = explode("\r", $contents);
	$line = $word1[rand(0, count($word1)-1)];

echo "<p class='res'>";
printf($line, $word);
echo "</p><br />";

}

}

The only problem is, it is echoing all the lines at the same time, instead of plucking out just 1 at random

 

That's not possible because i randomly select one line (as file reads a file into an array) store it in $rand and retrieve that row by using $sentences[$rand] If it would however select all rows then you would get an error from printf saying something like: too few arguments.

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.