You are here: Re: Undefined offset notice with explode « PHP Programming Language « IT news, forums, messages
Re: Undefined offset notice with explode

Posted by Jerry Stuckle on 10/22/94 11:17

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.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация