473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 10162
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*******@attglobal.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($line),':') === 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
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...
2
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...
1
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:...
3
by: pareshgoel | last post by:
class B { virtual ~B(); } class D:public B { virtual ~D(); }
4
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: ...
3
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...
9
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...
3
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...

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.