473,785 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Undefined offset notice with explode

Hi there,

I'm having a problem with PHP which I'm not sure how to best solve.
Here is the code:

$fp = fopen("comments .txt", "r");
while(!feof($fp ))
{
$line = fgets($fp, 1024);
list($key, $value) = explode(":", $line, 2);

if ($key == "something" )
{
...etc...etc...

The problem here is that if the line doesn't contain a : then there
isn't anything to populate $key and $value and so PHP reports "PHP
Notice: Undefined offset: 1". When I used to work with Perl, it
wouldn't report and rely on me handling the issue later if needs be.

What is the best way to get the explode line to not report a PHP notice
when it comes across a $line that isn't in the correct format?

Many thanks,

Richard.

Jul 17 '05 #1
4 10193
Richard Lawrence wrote:
Hi there,

I'm having a problem with PHP which I'm not sure how to best solve.
Here is the code:

$fp = fopen("comments .txt", "r");
while(!feof($fp ))
{
$line = fgets($fp, 1024);
list($key, $value) = explode(":", $line, 2);

if ($key == "something" )
{
...etc...etc...

The problem here is that if the line doesn't contain a : then there
isn't anything to populate $key and $value and so PHP reports "PHP
Notice: Undefined offset: 1". When I used to work with Perl, it
wouldn't report and rely on me handling the issue later if needs be.

What is the best way to get the explode line to not report a PHP notice
when it comes across a $line that isn't in the correct format?

Many thanks,

Richard.


if (strpos($line, ':') !== false)
list($key, $value) = explode(":", $line, 2);
else
whatever...

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Jul 17 '05 #2
@list($key, $value) = ....

Jul 17 '05 #3
*** Richard Lawrence wrote/escribió (31 May 2005 01:55:16 -0700):
list($key, $value) = explode(":", $line, 2);


Not as elegant as list but...
$pair=explode(" :", $line, 2);

if(count($pair) ==2){
echo $pair[0];
echo $pair[1];
}

--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Jul 17 '05 #4


Jerry Stuckle wrote:
Richard Lawrence wrote:
Hi there,

I'm having a problem with PHP which I'm not sure how to best solve.
Here is the code:

$fp = fopen("comments .txt", "r");
while(!feof($fp ))
{
$line = fgets($fp, 1024);
list($key, $value) = explode(":", $line, 2);

if ($key == "something" )
{
...etc...etc...

The problem here is that if the line doesn't contain a : then there
isn't anything to populate $key and $value and so PHP reports "PHP
Notice: Undefined offset: 1". When I used to work with Perl, it
wouldn't report and rely on me handling the issue later if needs be.

What is the best way to get the explode line to not report a PHP notice
when it comes across a $line that isn't in the correct format?

Many thanks,

Richard.


if (strpos($line, ':') !== false)
list($key, $value) = explode(":", $line, 2);
else
whatever...

if (strpos(trim($l ine),':') === false) $line = trim($line) . ':'; //
add a colon to the line if one isn't there after trimming off the
trailing CR/LF

Ken

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
108841
by: lawrence | last post by:
I just switched error_reporting to ALL so I could debug my site. I got a huge page full of errors. One of the most common was that in my arrays I'm using undefined offsets and indexes. These still work fine, but with error reporting at all they are marked as errors. Why? What am I doing wrong? www.monkeyclaus.org
2
25825
by: Steven | last post by:
Hi All, I am moving some php code from a Linux machine to a Windows 2000 machine with the code belowe I get the following error : Notice: Undefined offset: 1 in c:\inetpub\wwwroot\test.php on line 5 1) reset($kolom1); 2) while(list($cat,$lnk) = each($kolom1)){ 3) kop($cat);
1
4585
by: google | last post by:
Hello, I'm having major problems trying to get my head round this problem. I'm trying to generate an error free script, however, I still cannot sort out this loop. I get a "Notice: Undefined offset: 24 in c:\inetpub\wwwroot\classes\file.php on line 348"
3
1671
by: pareshgoel | last post by:
class B { virtual ~B(); } class D:public B { virtual ~D(); }
4
7196
by: John Oliver | last post by:
PHP Notice: Undefined index: name in /home/www/reformcagunlaws.com/new.php on line 6 PHP Notice: Undefined index: address in /home/www/reformcagunlaws.com/new.php on line 7 PHP Notice: Undefined index: city in /home/www/reformcagunlaws.com/new.php on line 8 PHP Notice: Undefined index: county in /home/www/reformcagunlaws.com/new.php on line 9 PHP Notice: Undefined index: zip in /home/www/reformcagunlaws.com/new.php on line...
3
4966
cassbiz
by: cassbiz | last post by:
Here are the errors that are coming up in my error_log Notice: Undefined index: andatum in /zipcode.php on line 11 Notice: Undefined index: andatum in /zipcode.php on line 12 Notice: Undefined index: abdatum in /zipcode.php on line 13 Notice: Undefined index: zimmer in /zipcode.php on line 14 Notice: Undefined index: city in /zipcode.php on line 39 Notice: Undefined index: state in /zipcode.php on line 41
9
32506
by: simple12 | last post by:
Hello I have a script which have the facility of entering any code to some part of a webpage. I have some problems with it. When i put some code in the script then their is no error shown. When i try to not use a code and leave it empty the following php errors happens. I dont understand why. Help me. Error shown are: - PHP Notice: in file /index.php on line 132: Undefined offset: 4 PHP Notice: in file /index.php on line 141:...
3
3358
by: anwar80 | last post by:
I just download dreamweaver extension for image upload. and run, It have some error... can anybody help... pls.. i have php 4.3.10. Notice: Undefined index: myimage in c:\program files\easyphp1-8\www\karsazestate\upload.php on line 20 <?php // --------------------------------------------- // Pure PHP Upload version 1.1 // ------------------------------------------- if (phpversion() > "4.0.6") { $_POST = &$_FILES; } ...
2
2690
by: neridaj | last post by:
Hello, I'm trying to figure out how to get rid of these errors: Notice: Undefined offset: 1 in output_fns.php on line 315 Notice: getimagesize() : Read error! in output_fns.php on line 315 function get_imgdim()
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.