Page 1 of 1
Undefined offset: 0 in ...
Posted: Mon Apr 16, 2012 4:08 am
by roeio
Hey,
No matter what i'm trying to do I get the following error:
Notice: Undefined offset: 0 in /var/www/html/new.php on line 54
Notice: Undefined offset: 0 in /var/www/html/new.php on line 54
Notice: Undefined offset: 0 in /var/www/html/new.php on line 54
Notice: Undefined offset: 0 in /var/www/html/new.php on line 54
Here is the raw code:
Code: Select all
if (preg_match ( '/(?<=">).*(?=<\/revision>)/', $xml, $revision ) == 0) {
$revision = array(0 => "--");
}
preg_match ( '/(?<=parent=").*(?=" uid="\d+")/', $xml, $machineID );
preg_match("/\d+/", $machineID[0], $ip);
The last line is line #54, what it supposed to do is take the name taken out in the previous line (ex. roy02) and assign only the number to a new varible called ip.
So when the loop "runs" on the array it should print out only 02 03 04 05 and I won't get this stup** error everytime...
Please Advise,
Roy.
Re: Undefined offset: 0 in ...
Posted: Mon Apr 16, 2012 5:17 am
by social_experiment
what does this code display?
Code: Select all
<?php
preg_match ( '/(?<=parent=").*(?=" uid="\d+")/', $xml, $machineID );
print_r($machineID);
// preg_match("/\d+/", $machineID[0], $ip);
?>
Undefined means that the value you are trying to use doesn't exist
Re: Undefined offset: 0 in ...
Posted: Mon Apr 16, 2012 5:49 am
by roeio
It runs on local hosts and displays the outout (numbers them down).
Code: Select all
preg_match ( '/(?<=parent=").*(?=" uid="\d+")/', $xml, $machineID );
if I run print_r($machineID); here this is my output (for example):
roy01
roy02
roy03
(It runs on the loop).
What I want it to do is just display the number, output should be:
01
02
03
From what I know about PHP (and it is really not that much

This should do the work:
Code: Select all
preg_match("/\d+/", $machineID[0], $ip);
It extracts the numbers from the "roy01..." and sends to $ip only the number.
The thing is that it does sends the numbers but I still get the error...
For example, PHP Code:
Code: Select all
preg_match ( '/(?<=parent=").*(?=" uid="\d+")/', $xml, $machineID );
preg_match("/\d+/", $machineID[0], $ip);
print_r($ip);
output on my screen is:
[text]Notice: Undefined offset: 0 in /var/www/html/new.php on line 52
Array ( ) Array ( [0] => 02 ) Array ( [0] => 03 ) Array ( [0] => 04 ) Array ( [0] => 05 ) Array ( [0] => 06 ) Array ( [0] => 07 ) Array ( [0] => 08 ) Array ( [0] => 12 ) Array ( [0] => 14 )
Notice: Undefined offset: 0 in /var/www/html/new.php on line 52
Array ( )
Notice: Undefined offset: 0 in /var/www/html/new.php on line 52
Array ( )
Notice: Undefined offset: 0 in /var/www/html/new.php on line 52
Array ( )[/text]
Thanks,
Roy.
Re: Undefined offset: 0 in ...
Posted: Mon Apr 16, 2012 5:56 am
by social_experiment
can you supply some test data for me to test against, one or two lines should be sufficient
Re: Undefined offset: 0 in ...
Posted: Mon Apr 16, 2012 6:15 am
by roeio
Code: Select all
// Setting Global varibles.
$file = 'status.xml';
$basePath=$_SERVER['DOCUMENT_ROOT'].'/'; //'/var/www/html/';
$xmlSearch= file_get_contents($basePath.$file);
$arr = scandir('/tmp/webber');
for($i = 0; $i < $testers_total; $i ++) {
$filename = $arr[$i];
$xml = "";
$f = fopen ( '/tmp/webstatus/' . $filename, 'r' );
while ( $data = fread ( $f, 4096 ) )
$xml .= $data;
fclose ( $f );
if (0 == preg_match_all ( '/(?<=title=")(.*)(?=" id=")/', $xml, $test_case )) {
$test_case = array(0 => "-------");;
}
$test_case = array_unique ( $test_case );
if (is_array ( $test_case [0] )) {
if (count( $test_case ) == 1) {
$testcase = array (0 => $test_case [0][count($test_case[0])-1] );
}
}
if (preg_match ( "/PNX.*(?=<\/version>)/", $xml, $version ) == 0) {
$version = array(0 => "-");
}
if (preg_match ( '/(?<=">).*(?=<\/revision>)/', $xml, $revision ) == 0) {
$revision = array(0 => "--");
}
preg_match ( '/(?<=parent=").*(?=" uid="\d+")/', $xml, $machineID );
preg_match("/\d+/", $machineID[0], $ip);
print_r($ip);
}
the XML file should look like (it parses info from the XML file)
[text]
<?xml version="1.0"?>
<Hosts>
<Roy01><ID>SN1231</ID><Phase>Backup</Phase><Version>CentOS 5</Version><Kernel>Xen</Kernel><Status>Running</Status>
</Hosts>
[/text]
Thanks,
Roy.
Re: Undefined offset: 0 in ...
Posted: Mon Apr 16, 2012 7:34 am
by amyhughes
Your regex isn't working. At least for the xml you provided only $test_case has something other than an empty array, and it contains an array that has two empty arrays in it.
PHP has xml functions that make this a lot easier.
Code: Select all
<?php
$xml = '<?xml version="1.0"?>\n<Hosts>\n<Roy01><ID>SN1231</ID><Phase>Backup</Phase><Version>CentOS 5</Version><Kernel>Xen</Kernel><Status>Running</Status>\n</Hosts>';
$x = array("oioioi");
print_r($x);
preg_match_all ( '/(?<=title=")(.*)(?=" id=")/', $xml, $test_case );
print_r($test_case);
preg_match ( "/PNX.*(?=<\/version>)/", $xml, $version );
print_r($version);
preg_match ( '/(?<=">).*(?=<\/revision>)/', $xml, $revision );
print_r($revision);
preg_match ( '/(?<=parent=").*(?=" uid="\d+")/', $xml, $machineID );
print_r($machineID);
?>
Re: Undefined offset: 0 in ...
Posted: Mon Apr 16, 2012 8:10 am
by roeio
Yeah but the thing is that it parses its information from a file.
in you're example Where does it collect the info from?
Re: Undefined offset: 0 in ...
Posted: Mon Apr 16, 2012 1:28 pm
by amyhughes
The script you posted reads a file into a string called $xml. I just skipped the file part and put the sample xml file you posted in a string called $xml. You'd get the same results reading it from a file.
The newlines in it aren't processed as newlines since they're in single quotes, but it doesn't change anything to take them out or put them in double quotes. The regex just isn't working as you intended.
Re: Undefined offset: 0 in ...
Posted: Mon Apr 16, 2012 5:07 pm
by social_experiment
Code: Select all
<?php
while ( $data = fread ( $f, 4096 ) )
$xml .= $data;
fclose ( $f );
?>
What's inside the variable $xml? I tested the code and this is inside the variable
Code: Select all
SN1231 Backup CentOS 5 Xen Running
Also have a look at the XML, the <Roy01> needs a terminating tag or the document isn't valid.
http://www.xmlvalidation.com
Re: Undefined offset: 0 in ...
Posted: Tue Apr 17, 2012 2:10 am
by roeio
Yeah the <Roy01> has an ending at the end of the line </Roy01>
Re: Undefined offset: 0 in ...
Posted: Thu Apr 19, 2012 5:00 pm
by social_experiment
If you modify your code to include the sample below what happens?
Code: Select all
<?php
if (is_array($machineID)) {
preg_match("/\d+/", $machineID[0], $ip);
}
?>