?
Solved

php text file parsing

Posted on 2007-10-15
7
Medium Priority
?
174 Views
Last Modified: 2013-12-13
Dear experts,

I am parsing a text file I upload, but  I do not get the full line, only the first character is shown and not from the right line, and I get some nasty errors like : "Notice: Uninitialized string offset: 14 in c:\program files\easyphp1-8\www\content\upload\_upload_process.php on line 45"

My code is:
//file handle
$handle = fopen($filename, "r");
if(!$handle)
{
 echo "An error has occured while opening file.";
}
else
{
 $line=fgets($handle);
 $j=0;
 $error="false";
 while(($line=fgets($handle)) !== false)
 {
 $line[$j];

   echo $line[$j]."<br>";
 $j++;
 }
}
Any ideas?
0
Comment
Question by:ChoobsTech
  • 5
  • 2
7 Comments
 
LVL 2

Author Comment

by:ChoobsTech
ID: 20080314
I want to show only some lines of the text file, where for example "name" matches...

I know how to match, but it doesn't print what I want :(
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 20080333
Try:

 while(($line=fgets($handle)) !== false) {
    if (strpos($line, 'name') !== false) {
        echo $line;
    }
 }

fgets return the line read and this a complete string.
The given example will display each line which contain the string 'name' (case sensitive)
0
 
LVL 2

Author Comment

by:ChoobsTech
ID: 20080841
Thanx you very much, it really helps :D
But how do I show full lines under it?

I need to print out all the (full) lines between "name" and "end of details"...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:ChoobsTech
ID: 20080851
How can I access line through numbers?
0
 
LVL 48

Expert Comment

by:hernst42
ID: 20080875
ok in this case somthing like this (untested):

$startfound = false;
while(($line=fgets($handle)) !== false) {
    if (strpos($line, 'name') !== false) {
        $startfound = true;
        continue;
    }
    if (strpos($line, 'end of details') !== false) {
        $startfound = false;
        continue;
    }
     if (!$startfound) {
        continue;
     }
     echo $line;
 }
0
 
LVL 2

Author Comment

by:ChoobsTech
ID: 20081683
Hey ;) Thnx for the input :)

It works fine... It's only that I need to be able to select more than 1 line for some tests...

How do I select a line with a number?
0
 
LVL 2

Author Comment

by:ChoobsTech
ID: 20082406
Nevermind I found a way ;)

Thnx for the help :D Going for grade A.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question