Jump to content

(?) PHP Notice: Undefined offset: 1


leke

Recommended Posts

Hi, I'm testing my PHP in the console and was wondering what this means?

PHP Notice: Undefined offset: 1

Is it a warning or an error?

My code...

$form_submission = "brother";
$lines = file('eo_dic.txt');

if ($form_submission == null){ // Sanitization. 
	echo 'Text field is empty!';
	exit;
}
  
foreach ($lines as $line) {

list($field1, $field2) = preg_split('/=/', $line); // back slash might not be needed

  if (stristr($field1, $form_submission) || stristr($field2, $form_submission)){
	echo $field1 . ' = ' . $field2;
}
}

The results and error...

Brother = frato
Brotherhood = frateco
Brotherly = frata
PHP Notice:  Undefined offset: 1 in /home/asdf/Dev/Projects/eo_translate/script_part2.php on line 50
PHP Notice:  Undefined offset: 1 in /home/asdf/Dev/Projects/eo_translate/script_part2.php on line 50
PHP Notice:  Undefined offset: 1 in /home/asdf/Dev/Projects/eo_translate/script_part2.php on line 50
PHP Notice:  Undefined offset: 1 in /home/asdf/Dev/Projects/eo_translate/script_part2.php on line 50

Line 50 = list($field1, $field2) = preg_split('/=/', $line);

Link to comment
https://forums.phpfreaks.com/topic/233185-php-notice-undefined-offset-1/
Share on other sites

if you have a look at here: http://php.net/manual/en/function.preg-split.php

you will find that preg_split returns matches in a DOUBLE array, so you will not be matching $field1 and $field2 to strings, but ARRAYS:

$form_submission = "brother";
$lines = file('eo_dic.txt');

if ($form_submission == null){ // Sanitization. 
	echo 'Text field is empty!';
	exit;
}
  
foreach ($lines as $line) {

list($field1, $field2) = explode('=', $line); // back slash might not be needed

  if (stristr($field1, $form_submission) || stristr($field2, $form_submission)){
	echo $field1 . ' = ' . $field2;
}
}

I am assuming "=" is your delimiter (without the double quotes).

Ted

Can you change to the following and show me the output:

$form_submission = "brother";
$lines = file('eo_dic.txt');

if ($form_submission == null){ // Sanitization. 
	echo 'Text field is empty!';
	exit;
}
  
foreach ($lines as $line) {echo "||||$line||||";

list($field1, $field2) = explode('=', $line); // back slash might not be needed

  if (stristr($field1, $form_submission) || stristr($field2, $form_submission)){
	echo $field1 . ' = ' . $field2;
}
}

// I realized I didn't really need the sanitation part for the example...

Ok, this...

$form_submission = "brother";
$lines = file('eo_dic.txt');
  
foreach ($lines as $line) { echo "||||$line||||";

list($field1, $field2) = explode('=', $line); 

  if (stristr($field1, $form_submission) || stristr($field2, $form_submission)){
	echo $field1 . ' = ' . $field2;
}
}

Returned this...

...The output of the dictionary -- which my console only caught the end of -- which I've also shortened because it looks the same from the letter "V"...

||||||||Zany=ŝercemulo

||||||||Zeal=fervoro

||||||||Zealot=fervorulo

||||||||Zealous=fervora

||||||||Zebra=zebro

||||||||Zenith=zenito

||||||||Zephyr=venteto

||||||||Zero=nulo

||||||||Zest=gusto

||||||||Zigzag=zigzago

||||||||Zinc=zinko

||||||||Zinc-worker=zinkisto

||||||||Zodiac=zodiako

||||||||Zone=terzono

||||||||Zoology=zoologio

||||||||Zoophyte=zoofito

||||||||Zouave=zuavo

||||

 

 

Try this, I can't seem to find any errors beside the new line thing:

$form_submission = "brother";
$lines = file('eo_dic.txt');

if ($form_submission == null){ // Sanitization. 
	echo 'Text field is empty!';
	exit;
}
  
foreach ($lines as $line) {
        $line = str_replace("\n", "", $line); $line = str_replace("\r", "", $line);
list($field1, $field2) = explode('=', $line); // back slash might not be needed

  if (stristr($field1, $form_submission) || stristr($field2, $form_submission)){
	echo $field1 . ' = ' . $field2;
}
}

Oh wait, I changed my console settings so it included unlimited lines and while I was scrolling through the dictionary, I discovered a empty lines where the script had errored.

Just going through and correcting now.

oh, in that case

replace the second line with this

$lines = file('eo_dic.txt', FILE_SKIP_EMPTY_LINES);

http://php.net/manual/en/function.file.php

this skips the empty line for u.

this is under the circumstance that you have \n at the end of each line:

$lines = file('eo_dic.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

Ted

I was still getting the error with...

$line = str_replace("\n", "", $line);

$line = str_replace("\r", "", $line);

 

...and also with just...

FILE_SKIP_EMPTY_LINES

or

FILE_IGNORE_NEW_LINES

 

...but when I tried both FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES

...I didn't get any errors. The output looked like this,

Brother = fratoBrotherhood = fratecoBrotherly = frata

but I can fix that with some formatting I guess.

 

It's pretty weird. I actually fixed all the empty lines I found in the text file(edit: whoops I missed some, so I guess it was the empty lines after all), but the errors still occurred until I added FILE_IGNORE_NEW_LINES and FILE_SKIP_EMPTY_LINES.

I'll look at the manual for FILE_IGNORE_NEW_LINES and  FILE_SKIP_EMPTY_LINES and see how they fixed the problem.

 

Thanks for the help bud.

 

I was still getting the error with...

$line = str_replace("\n", "", $line);

$line = str_replace("\r", "", $line);

 

...and also with just...

FILE_SKIP_EMPTY_LINES

or

FILE_IGNORE_NEW_LINES

 

...but when I tried both FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES

...I didn't get any errors. The output looked like this,

Brother = fratoBrotherhood = fratecoBrotherly = frata

but I can fix that with some formatting I guess.

 

It's pretty weird. I actually fixed all the empty lines I found in the text file(edit: whoops I missed some, so I guess it was the empty lines after all), but the errors still occurred until I added FILE_IGNORE_NEW_LINES and FILE_SKIP_EMPTY_LINES.

I'll look at the manual for FILE_IGNORE_NEW_LINES and  FILE_SKIP_EMPTY_LINES and see how they fixed the problem.

 

Thanks for the help bud.

 

Thats the correct output, if you want to separate them you HAVE to use html code:

$form_submission = "brother";
$lines = file('eo_dic.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

if ($form_submission == null){ // Sanitization. 
	echo 'Text field is empty!';
	exit;
}
  
foreach ($lines as $line) {

list($field1, $field2) = explode('=', $line); // back slash might not be needed

  if (stristr($field1, $form_submission) || stristr($field2, $form_submission)){
	echo $field1 . ' = ' . $field2 . '<br />';//NEED tO USE HTML CODE
}
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.