Skip to content
Advertisement

explode() offset and delimiter warnings when delimiter is valid

I’m having a heck of a time trying to figure out what is going on. I have a block of code that is throwing some errors. Outside of the Warnings and Notices the code works fine and the arrays are not empty. But why I am getting those warnings and notices is bothersome. In my test environment I don’t get the errors however when put on a different server they pop right up. I am stumped as to why. The lines of code that are throwing the errors and the errors being thrown are as follows

 $libTitle = explode($libYearRes[0], $value);
 Notice: Undefined offset: 0 
 Warning: explode(): Empty delimiter

 $sortLibYearRes = explode(' ', $libYearRes[0]);
 Notice: Undefined offset: 0

 $libMovieRes = $sortLibYearRes[1];
 Notice: Undefined offset: 1

The entire block of code is as follows with the problem lines denoted with a @ in them

 function checkMovieLibrary($movieTitle,$movieLibrary){

   foreach ($movieLibrary as $value) {
      preg_match('/(dddd)sddd*[Pp]/', $value, $libYearRes);
      $libTitle = @explode($libYearRes[0], $value);
      $libMovieTitle = rtrim($libTitle[0]);
      $sortLibYearRes = @explode(' ', $libYearRes[0]);
      //list($libMovieYear,$libMovieRes) = explode(' ', $libYearRes[0], 2);
      $libMovieYear = $sortLibYearRes[0];
      $libMovieRes = @$sortLibYearRes[1];

      preg_match('/(dddd)sddd*[Pp]/', $movieTitle, $newfileYearRes);
      $newFileTitle_array = explode($newfileYearRes[0], $movieTitle);
      $newFileTitle = rtrim($newFileTitle_array[0]);
      $sortFileYearRes = explode(' ', $newfileYearRes[0]);
      $newFileMovieYear = $sortFileYearRes[0];
      $newFileMovieRes = $sortFileYearRes[1];

      if(stripos($libMovieTitle.' '.$libMovieYear, $newFileTitle.' '.$newFileMovieYear) !== false){
          $moviesList_array[] = array('OriginalFile' => $value, 'NewFile' => $movieTitle);
          return($moviesList_array);

      }
  }
  return false;

}

//$movieTitle = "10 Cloverfield Lane (2016) 1080p.mkv"; //This is an example of $movieTitle string
//$movieLibrary = array('10 Cloverfield Lane (2016) 1080p.mkv','The Goonies (1985) 1080p.mp4'); this is an example of the $movieLibrary array

$doesMovieExist = checkMovieLibrary($Movie,$movieLibrary);

if(is_array($doesMovieExist) && $doesMovieExist !== false){
    foreach($doesMovieExist as $fileKey => $fileValue) {
        $originalFile = $fileValue['OriginalFile'];
        $newFile = $fileValue['NewFile'];
    }

   //do some stuff here
 }

Advertisement

Answer

First of all dont use @ because it will bypass the error message. So user error_reporting(E_ALL) till development phase.

Then you are getting undefined offset it means your array is empty of does not exist so put a condition to check if it exist then the explode should work. It must be like that :

if(!empty($libYearRes[0]) || isset($libYearRes[0])) {
     $libTitle = @explode($libYearRes[0], $value);
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement