Page 1 of 1
need help with this small code!
Posted: Wed Jun 11, 2008 6:00 pm
by nicosns
Hello,
I'm still a newbie when it comes to PHP, so I need help to correct this code.
What does it do?
It opens a file (file.txt), then it looks if there are duplicates en when they do the code will hide one or more of the double ones, and final it shows the code without the dups + ONLY the last 10 from the list. But the code is not finished, so I get errors. It must be something wth the 'echo implode' thing.
Code: Select all
<?php
$file = "file.txt";
$dups = array_unique (explode ("\n", str_replace("\n<a href=\x22http://www.domain.com/start/\x22></a></br>", "", file_get_contents($file))));
echo implode ("\n", $dups)("",array_slice(file("$file"),-10,10));
?>
I hope someone can tell me what I'm doing wrong. Thanks!
Re: need help with this small code!
Posted: Wed Jun 11, 2008 9:08 pm
by hansford
the code's do jumbled it's hard to tell what you're trying to do. Why not break some of those functions down so it's more readable. Also, this 'code' opens a file and looks for duplicates of what? Then it's suppose to do exactly what?
Re: need help with this small code!
Posted: Wed Jun 11, 2008 9:11 pm
by superdezign
nicosns wrote:Code: Select all
echo implode ("\n", $dups)("",array_slice(file("$file"),-10,10));
Syntax error.
Code: Select all
("",array_slice(file("$file"),-10,10));
The second part seems to be a stray.
Re: need help with this small code!
Posted: Thu Jun 12, 2008 11:30 am
by nicosns
I tried to combine 2 codes...
file.txt contains something like this:
marc
peter
ellen
martin
petra
...
------------------------------------------
Code 1: includes 'file.txt' en shows only the last 10 lines from that text file.
Code: Select all
<?php
$file = "file.txt";
echo implode("",array_slice(file("$file"),-10,10));
?>
-------------------------------
code2: includes 'file.txt', looks for duplicates + replaces something, and then it show the WHOLE list (without dups).
Code: Select all
<?php
$file = "file.txt";
$dups = array_unique (explode ("\n", str_replace("\n | ", "", file_get_contents($file))));
echo implode ("\n", $dups);
?>
************************
Now I want to combine this 2 codes so I'll end with:
inlcudes 'file.txt', looks for duplicates + replaces something, and then it shows only the 10 last lines (now without dups). But I have a problem with the 2 'echo implodes'. How do you combine 2 echo implodes from 2 codes?
Code: Select all
<?php
$file = "file.txt";
$dups = array_unique (explode ("\n", str_replace("\n<a href=\x22http://www.domain.com/start/\x22></a></br>", "", file_get_contents($file))));
echo implode ("\n", $dups)("",array_slice(file("$file"),-10,10));
?>
Re: need help with this small code!
Posted: Fri Jun 13, 2008 7:22 pm
by nicosns
Anyone can help with this small code, please?
Re: need help with this small code!
Posted: Fri Jun 13, 2008 8:50 pm
by WebbieDave
I'm not sure why you need to do the following:
Code: Select all
str_replace("\n<a href=\x22http://www.domain.com/start/\x22></a></br>", "", file_get_contents($file));
This will result in a parse error:
Code: Select all
echo implode ("\n", $dups)("",array_slice(file("$file"),-10,10));
To get the last ten elements of an array, try this:
Code: Select all
$dupsCount = count($dups); // should really be called nodups
if ($dupsCount > 10) {
$offset = $dupsCount - 10;
} else {
$offset = 0;
}
$lastTen = array_slice($dups, $offset);