Hi all,
Just a dumb logic error that has me somewhat stumped.
I want to produce a bit of output that shows the words stored in a text file before and after the word im working with.
The text file looks like:
word 1
word 2
word 3
word 4
word 5
The output that I would like should look like:
word 1 - word 2
word 1 - word 2 - word 3
word 2 - word 3 - word 4
word 3 - word 4 - word 5
word 4 - word 5
Now the code I thought I could use to produce the above was:
<?PHP
$lines = file("keywords.txt");
foreach ($lines as $value)
{
echo prev($value) . " - ";
echo $value . " - ";
echo next($value) . "<br>";
}
?>
If I comment out the line: echo prev($value) . " - "; then it kinda works.
I have tried multiple combinations of the above code but cant get it to work...
It will work if I use:
<?PHP
$lines = file("keywords.txt");
for($i=1;$i<count($lines);$i++)
{
echo trim($lines[($i-1)]) . " - ";
echo trim($lines[$i]). " - ";
echo trim($lines[($i+1)]) . "<br>";
}
?>
But I cant see why the previous wouldnt work using the built in prev() and next() functions to move the variable pointer.
Any ideas 🙂