By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,770 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,770 IT Pros & Developers. It's quick & easy.

Undefined offset notice with explode

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
@list($key, $value) = ....

Jul 17 '05 #3

P: n/a
*** 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

P: n/a


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 discussion thread is closed

Replies have been disabled for this discussion.