NameSilo

Process text files using PHP

Spaceship Spaceship
Watch

Wuoshi

Established Member
Impact
8
I've been working on the following snippet:



PHP:
$lines = file('http://www.example.com/thefile.txt');


foreach ($lines as $line_num => $line)
{
    echo substr($line,0,strpos($line,' ')), "<br/>\n";
}


The snippet fetches the first word on each line in the textfile.

What I need to do is to check if the word on the line is already collected, since I only want one occurance of each word. Anybody got any ideas?

Thank you!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
PHP:
<?php

$lines = file('file.txt');
$words = array();
foreach ($lines as $line)
{
  $line = explode(" ", $line);
  $word = $line[0];
  if (!in_array($word, $words)) $words[] = $word;
}

?>
 
0
•••
Thank you! Great solution!
 
0
•••
If you are looking for distinct values you can apply array_unique() function to your array
 
0
•••
Wow! That was easy, thanks alot!
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back