Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

How parse a txt file?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

How parse a txt file?

Postby raysefo » Mon Jun 03, 2013 9:18 am

Hi,

I am new to PHP.So any help would be great. I would like to display a drop down list and a textbox on a page. This dropdown data will be populated from a textbox. What I would like to know is,

how to:
    parse a file and extact some data from a txt file which is on a server.
    populate this dropdown on the page with the data
    also I would like to display another data on the textbox according to the data selected on the drop down list

and of couse which how to test this script etc.

Best Regards.
raysefo
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Jan 17, 2011 10:19 am

Re: How parse a txt file?

Postby johnj » Tue Jun 04, 2013 12:45 am

open the text file, read line by line, search for what you want and move to the next line in the file till you reach end of the file.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: How parse a txt file?

Postby pbs » Tue Jun 04, 2013 12:49 am

use php file() function, it will return you file content in array format.

refer: http://php.net/manual/en/function.file.php
pbs
New php-forum User
New php-forum User
 
Posts: 39
Joined: Sat May 11, 2013 1:40 am
Location: Nashik, India

Re: How parse a txt file?

Postby raysefo » Tue Jun 04, 2013 3:53 am

Hi,

Here is my sample script which works for local files;
Code: Select all
<?php
$homepage = file_get_contents('d:\php\test.txt');


$kw = explode("\n", $homepage);
$totalNumRow = 0;
$totalAmount = 0;
 
for($i=0;$i<count($kw);$i++){
   
   
   if(substr($kw[$i], 0, 1) == "5")
   {
      echo $kw[$i];
      // echo "<br>";
      $totalNumRow = $totalNumRow + 1;
      
      $originalstring = $kw[$i];
        $delimiter = ",";
        if(strpos($originalstring,$delimiter) > 0){
                $outarray = explode($delimiter,$originalstring);
                // $variable1 = $outarray[0];
                $amount = $outarray[1];
            echo $amount;
            $totalAmount = $totalAmount + $amount;
        }
      
      echo "<br>";
   }
   
   
}
echo "<br>";
echo "Total records: ".$totalNumRow;
echo "<br>";
echo "Total amount: ".$totalAmount;

?>


But the problem is I couldn't manage to get the file contents of a file on FTP. Is there a way to do it?

Best Regards.
raysefo
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Jan 17, 2011 10:19 am


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 9 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.