PDA

View Full Version : Strings as lines from other strings?



N!ck
09-06-2010, 03:28 PM
Edit: The title is confusing because I wrote it with a different idea in mine and forgot to change it :S.


Basically I have this PHP which takes a text file and outputs a random 8 lines from it. Now this is fine, except sometimes you get repeats which I do not want. I figure it will be complicated to take the code that I have and modify it so that it has no repeats.

What I want to do is make it shuffle the lines in the text file and then output the top 8 or something.

What I have:


<?php

function RandomLine($seed) {

$sites = file('/dir/to/file.txt');
$string = $sites[array_rand($sites)];

return $string;
}

$s0 = RandomLine(rand((float) microtime(), 1000003));
$s1 = RandomLine(rand((float) microtime(), 1000003));
$s2 = RandomLine(rand((float) microtime(), 1000003));
$s3 = RandomLine(rand((float) microtime(), 1000003));
$s4 = RandomLine(rand((float) microtime(), 1000003));
$s5 = RandomLine(rand((float) microtime(), 1000003));
$s6 = RandomLine(rand((float) microtime(), 1000003));
$s7 = RandomLine(rand((float) microtime(), 1000003));


$link = array($s0, $s1, $s2, $s3, $s4, $s5, $s7, $s7);

shuffle($link);

foreach ($link as $value)
{
$explode = explode('|', $value);
echo '<p>' . $explode[2] . ' <a href="' . $explode[0] . '">' . $explode[1] . '</a> ' . $explode[2] . '</p>';
}

?>


I want to take /dir/to/file.txt, shuffle it and feed the top 8 lines or whatever into my explode.

Source
09-06-2010, 03:36 PM
I'm embarrassed by this 30 sec code. But something like this should do what you want



<?php

$file = file_get_contents( '/dir/to/file.txt' );

$fileSegments = explode( '|', $file );
$fileSegments = shuffle( $fileSegments );

for ($i = 0; $i < 8 ; $i++) {
{
echo $fileSegments[$i];
}

?>


Maybe I mis-understood what you want. But that would take a file like:

This is a sentence.|This is another sentence, yay.|One More

You could easily change the explode to "\n" or something. Hope it helps.

N!ck
09-06-2010, 03:47 PM
I'm embarrassed by this 30 sec code. But something like this should do what you want



<?php

$file = file_get_contents( '/dir/to/file.txt' );

$fileSegments = explode( '|', $file );
$fileSegments = shuffle( $fileSegments );

for ($i = 0; $i < 8 ; $i++) {
{
echo $fileSegments[$i];
}

?>


Maybe I mis-understood what you want. But that would take a file like:

This is a sentence.|This is another sentence, yay.|One More

You could easily change the explode to "\n" or something. Hope it helps.

The file is more complex than that. It looks like:


http://url1.com|words that link to url1
http://url2.com|words that link to url2
http://url3.com|words that link to url3
http://url4.com|words that link to url4
http://url5.com|words that link to url5
etc...


The explode that I have does exactly what I want. Imagine this list has 20+ lines. I want to display a random 8 or so without any repeats.

What I think your does would shuffle my "http://urln.com|words that link to urln" around. And not look at the lines?

Source
09-06-2010, 03:48 PM
Well let me edit the script. You should have included the structure of your file in the original post...

Edit:

Here:



<?php

$file = file_get_contents( '/dir/to/file.txt' );

$fileLines = explode( "\n", $file );
$fileLines = shuffle( $fileLines );

for ($i = 0; $i < 8 ; $i++) {
{
$explode = explode( '|', $fileLines[$i] );
print_r( $explode ); // You can work the rest out yourself
}

?>


That would take



http://www.google.com|Awesome Search Engine
http://www.bing.com|Not So Awesome Search Engine


Randomise the lines, and then explode each line into seperate arrays.

N!ck
09-06-2010, 03:49 PM
Well let me edit the script. You should have included the structure of your file in the original post...

Yeah, sorry about that. I just thought I could keep the same explode so didn't think you needed the format.

Source
09-06-2010, 03:53 PM
I think my last post should now do what you want.

N!ck
09-06-2010, 04:46 PM
+rep'd you.

Struggling to make this work though. Getting PHP Notice: Undefined offset errors for the "echo" line



<?php

$file = file('/filehere.txt');

function explodeRows($file) {
$list = explode("\n", $file);
return $list;
}

shuffle($list);

for ($i = 1; $i < 8 ; $i++) {

$explode = explode('|', $list);
echo '<p>' . $explode[2] . ' <a href="' . $explode[0] . '">' . $explode[1] . '</a> ' . $explode[2] . '</p>';
}

?>

Source
09-06-2010, 05:48 PM
Probably because it looks nothing like the code I gave you. If the example file you showed earlier in the thread is correct then this WILL work:



<?php

$file = file_get_contents( '/dir/to/file.txt' );

$fileLines = explode( "\n", $file );
shuffle( $fileLines );

for ($i = 0; $i < 8 ; $i++)
{
$explode = explode( '|', $fileLines[$i] );
echo '<p><a href="' . $explode[0] . '">' . $explode[1] . '</a></p>';
}

?>


Ignor the fact I was defining the fileLines with a shuffle which was returning a bool... silly me!

http://cl.ly/ecfe725cae05c9490963

And the text file:


http://www.google.com|An awesome search engine
http://www.bing.com|A meh search engine
http://www.yahoo.com|A dead search engine

N!ck
09-06-2010, 06:08 PM
Probably because it looks nothing like the code I gave you. If the example file you showed earlier in the thread is correct then this WILL work:



<?php

$file = file_get_contents( '/dir/to/file.txt' );

$fileLines = explode( "\n", $file );
shuffle( $fileLines );

for ($i = 0; $i < 8 ; $i++)
{
$explode = explode( '|', $fileLines[$i] );
echo '<p><a href="' . $explode[0] . '">' . $explode[1] . '</a></p>';
}

?>


Ignor the fact I was defining the fileLines with a shuffle which was returning a bool... silly me!

http://cl.ly/ecfe725cae05c9490963

And the text file:


http://www.google.com|An awesome search engine
http://www.bing.com|A meh search engine
http://www.yahoo.com|A dead search engine

Yep, it's working great. Thanks!

eddiekins
15-06-2010, 11:27 AM
With all due respect, this is what databases were developed for. Why can't you use one?

N!ck
15-06-2010, 01:33 PM
With all due respect, this is what databases were developed for. Why can't you use one?

Why would I run a whole database service when I can use text files. I don't want the extra memory usage from a database sever and adding new lines to a text file will be far easier than updating a database ;).

eddiekins
15-06-2010, 03:21 PM
Why would I run a whole database service when I can use text files. I don't want the extra memory usage from a database sever and adding new lines to a text file will be far easier than updating a database ;).
Sound reasoning. If you're only doing like 8 at any one time, I guess it's fair enough to use text files... :)

Want to hide these adverts? Register an account for free!