Hi all,

I have a script with the following code. When I run it I get a warning:

Notice: Undefined offset: 1 in /home/vhosts/mydomain/httpdocs/cla/depart.php on line 166

Line 166 is: preg_split('#(?<=[a-z]) (?=\d)#i', $RemarksWithTimeTemp);

The content of "$RemarksWithTimeTemp" could be "Arrived 10:30", "Departed 11:21". There is always a number a alpha chars followed by a time.

Can anyone see why I am getting the warning.

if(empty($record["FLIGHT-REMARKSWITHTIME"])){
		$RemarksWithTime = "";
	}else{	 
		$RemarksWithTimeTemp =  $record["FLIGHT-REMARKSWITHTIME"];
		list($_SESSION['remark'], $_SESSION['time']) = 
      	preg_split('#(?<=[a-z]) (?=\d)#i', $RemarksWithTimeTemp); 
		$dTime = strlen($_SESSION['time']);
		if($dTime == 4){
			$RemarksWithTime = $_SESSION['remark']." ". "0".$_SESSION['time'];
		}else{
			$RemarksWithTime = $_SESSION['remark']. " " . $_SESSION['time'];
		}
	}	

    The problem is probably

    list($_SESSION['remark'], $_SESSION['time']) = preg_split('#(?<=[a-z]) (?=\d)#i', $RemarksWithTimeTemp); 
    

    When your preg_split can't find the pattern, it won't return an array with the index 1.

      Hi Undrium,

      Many thanks for your fast reply.

      Having looked at the data in more detail there are times when the content of "$RemarksWithTimeTemp" would not contain a time. Is there a way I can write the preg_split to look for the alpha chars if there is no time associated with the content of the var.

      Many thanks for any help you can provide.

        The string is split into an array with the first element being stuff left of whitespace and the second element being the stuff right of whitespace, if this whitespace is preceeded by a-z and followed by a decimal digit.

        Are you now asking how to split the string if it contains no digit after the whitespace (if there even is a whitespace)? Well, if there is no time, you'd have either "Arrived" or "Departed"... So just use that. You don't have to somehow split "Arrived" to get "Arrived".

        Still, if you don't want to check how many elements were returned, and if you do know that Arrived & Departed are always followed by a whitespace even when there is no time, i.e. 'Arrived ' and 'Departed ', you could of course use a lookahead alternative to time: end of string.

        Or, if there is no space after 'Arrived' and 'Departed' when the time part is missing, you could split on nothing, i.e. the nowhere between the lookbehind and the lookahead by using a lookahead of whitespace followed by digit or end of string, and then trim the time part which will either be whitespace followed by time or an empty string: (?<=[a-z])(?= \d|$)

          Write a Reply...