Jump to content

[SOLVED] 2 small codes make this newbie crazy!


nicob

Recommended Posts

:o I'm trying something out, but I can't get the right result.

 

I try 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.

 

<?php

$file = "file.txt";
echo implode("",array_slice(file("$file"),-10,10));

?>

 

-------------------------------

 

code2: includes 'file.txt', looks for duplicates + replaces something (important for me), and then it shows the WHOLE list without duplicates.

 

<?php

$file = "file.txt";
$nodups = array_unique (explode ("\n", str_replace("\n |  ", "", file_get_contents($file))));
echo implode ("\n", $nodups);

?>

 

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

Now I want to combine this 2 codes so I'll end with:

inlcudes 'file.txt', looks for duplicates + replaces something (important for me), 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 diff codes?

 

not working code with DOUBLE 'echo implode'

<?php
$file = "file.txt";
$nodups = array_unique (explode ("\n", str_replace("\n |  ", "", file_get_contents($file))));
echo implode ("\n", $nodups);
echo implode("",array_slice(file("$bestand"),-10,10));
?>

Instead of array_slice'ing the returned array from file(), slice the $nodups array:

 

<?php
$file = "file.txt";
$nodups = array_unique (explode ("\n", str_replace("\n |  ", "", file_get_contents($file))));
echo implode("\n",array_slice($nodups,-10,10));
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.