PHP Snippet of code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
notbob
Forum Newbie
Posts: 3
Joined: Sat Oct 11, 2003 3:27 pm

PHP Snippet of code

Post by notbob »

Hi

I'm wondering if someone might be able to provide me with a few lines of code to complete the following task in PHP.

I have three text files (a.txt, b.txt, c.txt), each containing text.

The script would randomly take a line of text from each file, merge them to make a phrase and hyperlink it to a url with dashes between the words. The output from the script would be vertical. Each time the script runs, the output would be different.

The script would have a variable x that could be set to say how many lines of output there would be.

Thanks for the help and I have an example below to clarify.

******************

Contents of a.txt

this
that
what

Contents of b.txt

will be
was
is

Contents of c.txt

good
bad
funny

*******************

In the php script, x=5

An example of the output of the script which is vertical consists of 5 hyperlinks.

<a href="http://www.url.com/what-will-be-funny.shtml">what will be funny</a>
<a href="http://www.url.com/this-is-good.shtml">this is good</a>
<a href="http://www.url.com/that-is-bad.shtml">that is bad</a>
<a href="http://www.url.com/what-was-funny.shtml">what was funny</a>
<a href="http://www.url.com/that-will-be-bad.shtml">this will be bad</a>
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Why don't you just use arrays() instead of txt files?

Code: Select all

<?php
$a = array("this","that","what");
$b = array("will be", "was", "is");
$c = array("good", "bad", "funny");

array_shuffle($a);
array_shuffle($b);
array_shuffle($c);

echo ('<a href="http://www.url.com/{$a[0]}-{$b[0]}-{$c[0]}.shtml">{$a[0]} {$b[0]} {$c[0]}</a>');
?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Gen-ik was on to something, if you still decide to use the filesystem.

Using file() you can read each line in a file into an array. Then you could work with it as in Gen-ik's post.
notbob
Forum Newbie
Posts: 3
Joined: Sat Oct 11, 2003 3:27 pm

Post by notbob »

Thank you very much.

The reason why I wanted to use text files instead of an array was that I find it is easier to make changes to the text file rather then an array.

I appreciate you taking the time to help me with this.

Jason
notbob
Forum Newbie
Posts: 3
Joined: Sat Oct 11, 2003 3:27 pm

Post by notbob »

Here's some code I received that does the same function. Different approach, same end result.



<?php

$links = 5;

$a = file('a.txt');
$b = file('b.txt');
$c = file('c.txt');


for ($i = 0; $i < $links; $i++) {

$link = $a[rand(0, sizeof($a)-1)] . '-' . $b[rand(0, sizeof($b)-1)] . '-' . $c[rand(0, sizeof($c)-1)];
echo "<a href=\"http://www.url.com/$link.shtml\">" . str_replace('-', ' ', $link) . "</a>\r\n";

}


?>
Post Reply