leke Posted April 9, 2011 Share Posted April 9, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/233185-php-notice-undefined-offset-1/ Share on other sites More sharing options...
ted_chou12 Posted April 9, 2011 Share Posted April 9, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/233185-php-notice-undefined-offset-1/#findComment-1199198 Share on other sites More sharing options...
leke Posted April 9, 2011 Author Share Posted April 9, 2011 Hi Ted, Thanks for the reply. Yeah, that made sense, so I used the explode function (which returns single array ), but get the same error as a result Quote Link to comment https://forums.phpfreaks.com/topic/233185-php-notice-undefined-offset-1/#findComment-1199232 Share on other sites More sharing options...
ted_chou12 Posted April 9, 2011 Share Posted April 9, 2011 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/233185-php-notice-undefined-offset-1/#findComment-1199234 Share on other sites More sharing options...
leke Posted April 9, 2011 Author Share Posted April 9, 2011 // 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 |||| Quote Link to comment https://forums.phpfreaks.com/topic/233185-php-notice-undefined-offset-1/#findComment-1199241 Share on other sites More sharing options...
ted_chou12 Posted April 9, 2011 Share Posted April 9, 2011 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/233185-php-notice-undefined-offset-1/#findComment-1199245 Share on other sites More sharing options...
leke Posted April 9, 2011 Author Share Posted April 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233185-php-notice-undefined-offset-1/#findComment-1199246 Share on other sites More sharing options...
ted_chou12 Posted April 9, 2011 Share Posted April 9, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/233185-php-notice-undefined-offset-1/#findComment-1199249 Share on other sites More sharing options...
leke Posted April 9, 2011 Author Share Posted April 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233185-php-notice-undefined-offset-1/#findComment-1199258 Share on other sites More sharing options...
ted_chou12 Posted April 9, 2011 Share Posted April 9, 2011 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 } } Quote Link to comment https://forums.phpfreaks.com/topic/233185-php-notice-undefined-offset-1/#findComment-1199462 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.