Strip a specific line of text from another fle

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
shanehunter
Forum Commoner
Posts: 30
Joined: Sun Jun 27, 2010 3:43 pm

Strip a specific line of text from another fle

Post by shanehunter »

So, here's my dilemma,

I'm calling a random line of text from another file, using the following code:

Code: Select all

	function RandomLine($seed) {
	$textfile = "links.txt";		
	$sites = array();
		if(file_exists($textfile)){		
		     $sites = file($textfile);		
		     srand ($seed);		
		     $string = $sites[array_rand($sites)];		
		} else {
		     $string = "Error";
		}
	return $string;
	}
	
	$s0 = RandomLine(rand((float) microtime(), 10000000));

echo $s0;
This code works well. But now, my next problem, is once that line is called, I would like to remove it from the file that it was called from, so that it will not duplicate.

Please help! =)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Strip a specific line of text from another fle

Post by requinix »

array_rand gives you a key in the array. Once you have that key (and the value associated with it) unset the item in the array and put the lines back into the original file.
shanehunter
Forum Commoner
Posts: 30
Joined: Sun Jun 27, 2010 3:43 pm

Re: Strip a specific line of text from another fle

Post by shanehunter »

haha...

i'm sorry, but i'm far too low on the IQ scale to understand what you just said. lol.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Strip a specific line of text from another fle

Post by requinix »

Right here

Code: Select all

$sites = file($textfile);
srand ($seed);
$string = $sites[array_rand($sites)];
it reads each line in the $textfile into the $sites array. Then it uses array_rand to locate the array key of a random line and puts that random line into $string.

Since $sites has everything in $textfile, and you want to remove that one line from the file, you can remove it from $sites instead and then save what's left back into $textfile.
If you hold onto the random key that array_rand gave you (by putting it into a variable rather than using it immediately) you can remove the line from $sites by using unset. To "save" the modification you then call file_put_contents.
shanehunter
Forum Commoner
Posts: 30
Joined: Sun Jun 27, 2010 3:43 pm

Re: Strip a specific line of text from another fle

Post by shanehunter »

so from that, I've got:

Code: Select all


$array[$s0] = $text; 
unset($sites[$text]); 

is that right? and what is an easy code to write the array back to a text file

minus the unset $text

?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Strip a specific line of text from another fle

Post by requinix »

Getting close.

Code: Select all

$lines = file($textfile); // as before
$key = array_rand($lines); // save this to a variable first
$string = $lines[$key]; // mostly as before

unset($lines[$key]); // remove it from the list
file_put_contents($textfile, $lines); // save the list back to the file
shanehunter
Forum Commoner
Posts: 30
Joined: Sun Jun 27, 2010 3:43 pm

Re: Strip a specific line of text from another fle

Post by shanehunter »

<edit> nevermind. i still don't get it...

lol. i apologize for the idiocy here, but I do believe I may be in over my head. :roll: :banghead:
Post Reply