Uninitialized string offset:

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
David Crabtree
Forum Newbie
Posts: 2
Joined: Mon Aug 09, 2010 10:34 pm

Uninitialized string offset:

Post by David Crabtree »

I keep getting an error:

PHP Notice: Uninitialized string offset: 7 in /home/reddingb/public_html/Instantleads/inc/isotope.php on line 14
PHP Notice: Uninitialized string offset: 6 in /home/reddingb/public_html/Instantleads/inc/isotope.php on line 14

Any ideas on what is wrong with the script to cause this?

Code: Select all

<?php

function isotope_load($tr,$fn) {
	$tn="__";
	$sb="";

	$f=fopen($fn,"r");
	while(1) {
		$l=fgets($f,1024);
		if($l && $l[0]!='[') { $sb.=$l; continue; }
		$s="";
		while($sb) {
			$i=strcspn($sb,'<');
			if($sb[$i]!='<'||$sb[$i+1]!='=') {
				$s.=substr($sb,0,$i+1);
				$sb=substr($sb,$i+1);
				if($sb) continue;
			}
			$s.=substr($sb,0,$i);
			if(!array_key_exists($tn,$tr)) $tr[$tn]=array();
			if($s) { array_push($tr[$tn],$s); $s=""; }
			
			$sb=substr($sb,$i);
			if(!$sb) break;
			$i=strcspn($sb,'>')+1;
			array_push($tr[$tn],substr($sb,0,$i));
			$sb=substr($sb,$i);
		}
		
		if(!$l) break;
		$i=strcspn($l,']');
		$tn=substr($l,1,$i-1);
	}
	fclose($f);
	return $tr;
}

function isotope_exec($tl, $tn, $vars) {
    global $dbc;
	$tr="";
	if(!array_key_exists($tn,$tl)) return "";
	foreach($tl[$tn] as $me) {
		if($me[0]!='<' || $me[1]!='=') { $tr.=$me; continue; }
		$n=substr($me,2); $n=substr($n,0,strcspn($n,'='));
		if(array_key_exists($n,$vars)) $tr.=$vars[$n];
        
	}
	return $tr;
}

?>
Thanks for your help!!!
David
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Uninitialized string offset:

Post by cpetercarter »

I think that this is line 14, right?

Code: Select all

if($sb[$i]!='<'||$sb[$i+1]!='=') {
At the beginning of your function you have:

Code: Select all

$sb = "";
In other words you initialise $sb as a simple string variable.
Subsequently you read into $sb the contents of the file $fb - so $sb is still a string variable.
But in line 14 you treat $sb as an array ($sb[$i]), and my guess is that php is telling you that you have not set $sb up as an array, so it cannot find the offsets represented by $i and $i+1.
My guess - and ths is purely a guess because I have no idea what your code is doing - is that you mean to add successive slices of $fb to $sb, not by concatenating them with $sb, but as successive array elements. If this is right, then you might use $sb[] for each new array element, and it would also be consistent at the beginning of the function to define $sb as an empty array:

Code: Select all

$sb = array();
PS I have just realised that this advice is quite wrong. It is, indeed, possible to access individual characters in a string with square brackets - so $string[0] is the first character in $string, $string[1] the second and so on. So I am mystified about the problem which your script has encountered.
Post Reply