LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-08-2010, 10:52 PM   #1
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,622
Blog Entries: 29

Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
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;
}

Last edited by TheIndependentAquarius; 11-10-2010 at 09:35 PM. Reason: Proper indentation
 
Old 11-08-2010, 11:11 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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?
 
1 members found this post helpful.
Old 11-10-2010, 07:44 AM   #3
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,622
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Thanks Paul,

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

Thanks again.
 
Old 11-10-2010, 07:26 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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

Last edited by graemef; 11-10-2010 at 07:31 PM.
 
Old 11-10-2010, 08:05 PM   #5
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,622
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Thanks graemef,

But I am unable to find any improper indentation
 
Old 11-10-2010, 08:13 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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
 
Old 11-10-2010, 09:40 PM   #7
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,622
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
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.

Last edited by TheIndependentAquarius; 11-10-2010 at 09:41 PM.
 
Old 11-10-2010, 10:05 PM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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;
}
 
Old 11-10-2010, 10:09 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
STL String: Uninitialized or some null value Ephracis Programming 9 03-04-2008 01:27 PM
Php String to Array joelhop Programming 2 12-18-2006 03:39 PM
LXer: Novell Receives Delisting Notice From NASDAQ and Notice of Default From Wells Fargo Related to its Convertible Subordinated Notes Due to Late Filing of Form 10-Q LXer Syndicated Linux News 0 09-21-2006 03:54 AM
Php String Handling joelhop Programming 1 09-11-2006 11:21 PM
PHP More than one space in a string Boffy Programming 4 05-01-2004 02:54 PM


All times are GMT -5. The time now is 06:53 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration