Jump to content

Notice: Undefined offset: 1 in line 60


moran1409

Recommended Posts

i get the error for this code:

 

 <?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

Link to comment
https://forums.phpfreaks.com/topic/228637-notice-undefined-offset-1-in-line-60/
Share on other sites

Thats no Hyphen....thats an equals (=) sign :) but nonetheless, it doesn't exist.

 

Try doing something like this:

<?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);

?>

 

Regards, PaulRyan.

What's in the file being read?

 

You could check to see if there are 2 items in the array before trying to print it:

<?php
$parts = explode('=', $line);
if (count($parts) == 2) {
   print $parts[0] . $parts[1]. "<BR>";
} else {
   print $parts[0] . "<br>";
?>

 

You could also, use the implode function

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

If you just want to print the line without the "=', just do

<?php
echo str_replace('=','',line) . '<br>';
?>

 

Ken

Thats no Hyphen....thats an equals (=) sign :) but nonetheless, it doesn't exist.

 

I either need glasses or I need to change my screen resolution, one of the two. Hell, possibly both.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.