LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Notice: Uninitialized string offset: 1 in [PHP] (http://www.linuxquestions.org/questions/programming-9/notice-uninitialized-string-offset-1-in-%5Bphp%5D-843135/)

TheIndependentAquarius 11-08-2010 11:52 PM

Notice: Uninitialized string offset: 1 in [PHP]
 
This code is giving me the above said error, this code was written by me 2 years back, can't remember the cause of error at this moment, please help !
Code:

function remove_spaces($input,$i)
{
  for($j=0;$input[$i];$i++)
  {
      if($input[$i] == ' ')
        continue;
      $input[$j] = $input[$i];
      $j++;
  }
  $input[$j] = '?';
  $input = strtok($input, "?");

  return $input;
}


paulsm4 11-09-2010 12:11 AM

Hi -

Off the top of my head, it sounds like this is the error:
Quote:

Notice: Uninitialized string offset...
Q: Did you initialize the string you're passing in to "remove_spaces()"?

Q: Your "for(;;)" loop terminates when - and only when - $input[0] is "0". Do you know if you're passing in a null terminated string?

TheIndependentAquarius 11-10-2010 08:44 AM

Thanks Paul,

I couldn't see this thread in my UserCP when you replied :mad:
I shall read out the mistakes you pointed out and probably report back.

Thanks again.

graemef 11-10-2010 08:26 PM

You should put some validation on $i, maybe default it to zero and certainly check that it is not greater than the length of the string passed in use strlen(). You may want to use the result of strlen() in your for loop terminator.

Also, please sort out your indentation, it's quite confusing ;)

TheIndependentAquarius 11-10-2010 09:05 PM

Thanks graemef,

But I am unable to find any improper indentation :confused:

graemef 11-10-2010 09:13 PM

Well indentation is a matter of personal style but I'd do it like this:

Code:

function remove_spaces($input,$i)
{
  for($j=0;$input[$i];$i++)
  {
      if($input[$i] == ' ')
        continue;
      $input[$j] = $input[$i];
      $j++;
  }
  $input[$j] = '?';
  $input = strtok($input, "?");

  return $input;
}

This is possibly how it appeared on you text editor but with the mixture of spaces and tabs it didn't appear very clean with your original post ;)

TheIndependentAquarius 11-10-2010 10:40 PM

remove_spaces function has been called as follows:
Code:

function parse_topics($input)
{
  $arr;$i=0;$j=0;
  $input = remove_spaces($input,$i);

and parse_topics function as follows (outside all functions):
Input to parse_topics is a comma separated string.
Code:

$Arr_topic_id  = parse_topics($_POST["txtTopic"]);
  • Is this info helpful in giving me more pointers ?
  • Previously I ran this code on PHP 3.5.1 and currently it is running on PHP 5.

graemef,
The indentation error you pointed out was negligible IMO, but for your sake I have modified the OP.

graemef 11-10-2010 11:05 PM

As was stated in the original response by paulsm4 is your string null terminated? I don't think so. Maybe it was in earlier versions of PHP but it would be better to change your code to use the strlen() function.

Code:

function remove_spaces($input,$i)
{
  $len = strlen($input);
  if ($i >= $len)
      return $input;

  for($j=0;$i < $len;$i++)
  {
      if($input[$i] == ' ')
        continue;
      $input[$j] = $input[$i];
      $j++;
  }
  $input[$j] = '?';
  $input = strtok($input, "?");

  return $input;
}


graemef 11-10-2010 11:09 PM

One more thought, if $i is not passed in as 0, say 10, then your returned string will start at that location (thus characters will be chopped off from the original string). If that is what you want fine, if not then you may want to change the initialise statement in the for loop from $j = 0 to $j = $i


All times are GMT -5. The time now is 04:03 AM.