Page 1 of 1

Notice: Undefined offset: 1 in line 60

Posted: Wed Feb 23, 2011 1:15 pm
by moran1409
i get the error for this code:

Code: Select all

	   <?php
		$news = fopen("test.txt", "rb");

		while (!feof($news) ) {

			$line = fgets($news);
			$parts = explode('=', $line);

		print $parts[0] . $parts[1]. "<BR>";//line 60!!!
		
	}

	fclose($news);

	   ?>
i know that this means that the array gets one argument instead of two but i do'nt know how to solve it...
this code works fine in my localhost but not on the server

Re: Notice: Undefined offset: 1 in line 60

Posted: Wed Feb 23, 2011 1:58 pm
by McInfo
What do you want your program to do with lines that do not contain "="?

Re: Notice: Undefined offset: 1 in line 60

Posted: Wed Feb 23, 2011 2:22 pm
by pickle
Basically it looks like you're just taking the '=' out right? Why not just do a str_replace()?

Re: Notice: Undefined offset: 1 in line 60

Posted: Wed Feb 23, 2011 3:17 pm
by moran1409
thanks for the help, i solved it and this is the solution:

Code: Select all

<?php

  $news = fopen("test.txt", "rb");

  while (!feof($news) ) {

    $line = fgets($news);
    $parts = explode('=', $line);

    if(count($parts) == 2) {
      echo $parts[0] , $parts[1], '<br>';
    } else {
      echo $parts[0], '<br>';
    }
               
  }

  fclose($news);

Re: Notice: Undefined offset: 1 in line 60

Posted: Wed Feb 23, 2011 4:05 pm
by pickle
You could replace

Code: Select all

if(count($parts) == 2) {
      echo $parts[0] , $parts[1], '<br>';
    } else {
      echo $parts[0], '<br>';
    }
with

Code: Select all

echo implode('',$parts).'<br />';