Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

Process text files using PHP

Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
SpaceshipSpaceship
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
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
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
Close
Thank you! Great solution!
 
0
Close
If you are looking for distinct values you can apply array_unique() function to your array
 
0
Close
Wow! That was easy, thanks alot!
 
0
Close
DomainEasy: flexible LTO options. Close more domain deals with financing.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back