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

reading a file

P: n/a
Hi,

I've got the following script:

<?
$inhoud = file("/opt/scripts/sc/sc_trans.log");
$inhoud = implode("", $inhoud);

$inhoud_array = explode("\n", $inhoud);

for($i=0; $i < count($inhoud_array); $i++){
$titel1 = explode("[DECODE] Opened ",$inhoud_array[$i]);
$titel2 = explode(".mp3",$titel1[1]);
$titel = $titel2[0];

$part1 = explode("<",$inhoud_array[$i]);
$part2 = explode(">",$part1[1]);
$tijd= $part2[0];
$tijd= ereg_replace("@"," ", $part2[0]);

echo "[$tijd] $titel\n";
}
?>

While running this I get:

PHP Notice: Undefined offset: 1 in /opt/scripts/shoutcast/crphp on
line 9

How do I solve this?

Gr. Patrick.

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Patrick wrote:
Hi,

I've got the following script:

<?
$inhoud = file("/opt/scripts/sc/sc_trans.log");
$inhoud = implode("", $inhoud);

$inhoud_array = explode("\n", $inhoud);
I guess you do this to remove the \n from each line... why don't you
trim() each line instead?

for($i=0; $i < count($inhoud_array); $i++){
$titel1 = explode("[DECODE] Opened ",$inhoud_array[$i]);
$titel2 = explode(".mp3",$titel1[1]);
$titel = $titel2[0];

$part1 = explode("<",$inhoud_array[$i]);
$part2 = explode(">",$part1[1]);
$tijd= $part2[0];
$tijd= ereg_replace("@"," ", $part2[0]);

echo "[$tijd] $titel\n";
}
?>

While running this I get:

PHP Notice: Undefined offset: 1 in /opt/scripts/shoutcast/crphp on
line 9
print_r($titel1);

My guess is you only get one element when you explode()
$inhoud_array[$i], and it will be $titel[0].

Without seeing a sample line from /opt/scripts/sc/sc_trans.log there's
little more to say.

How do I solve this?

Gr. Patrick.

Jul 17 '05 #2

P: n/a
On Fri, 14 Jan 2005 16:01:05 +0100, Dani CS
<co*****************@yahoo.es.quita-la-merluza> wrote:
Patrick wrote:
Hi,

I've got the following script:

<?
$inhoud = file("/opt/scripts/sc/sc_trans.log");
$inhoud = implode("", $inhoud);

$inhoud_array = explode("\n", $inhoud);


I guess you do this to remove the \n from each line... why don't you
trim() each line instead?

for($i=0; $i < count($inhoud_array); $i++){
$titel1 = explode("[DECODE] Opened ",$inhoud_array[$i]);
$titel2 = explode(".mp3",$titel1[1]);
$titel = $titel2[0];

$part1 = explode("<",$inhoud_array[$i]);
$part2 = explode(">",$part1[1]);
$tijd= $part2[0];
$tijd= ereg_replace("@"," ", $part2[0]);

echo "[$tijd] $titel\n";
}
?>

While running this I get:

PHP Notice: Undefined offset: 1 in /opt/scripts/shoutcast/crphp on
line 9


print_r($titel1);

My guess is you only get one element when you explode()
$inhoud_array[$i], and it will be $titel[0].

Without seeing a sample line from /opt/scripts/sc/sc_trans.log there's
little more to say.

How do I solve this?

Gr. Patrick.

This is how the logfile looks like:
<01/12/05@11:40:57> [STREAM] Connected to host server
<01/12/05@11:40:57> [DECODE] Opened 218 - Ali B. - Ik ben je zat.mp3
<01/12/05@11:40:57> [CONFIG] WARNING: No InputSamplerate defined,
assuming 44100!

So, I only need the line with [DECODE] in it.
var1 is the date and time
var2 is the text after [DECODE]

Jul 17 '05 #3

P: n/a
Patrick,

There is no $titel1[1] element.

Your first and third lines do not have the "[DECODE] Opened" patterns,
so explode returns in $titel1 the original string as an array...meaning
$titel1[0] == $inhoud_array[$i] and there is no $titel1[1]! That is why
PHP is complaining about undefined offset.

Since you doing some kind of pattern matching, you should probably look
into Regular Expressions. Please refer
http://us4.php.net/manual/en/function.preg-match.php

Thanks,
--Kartic

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.