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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-08-2010, 10:52 PM
|
#1
|
|
Senior Member
Registered: Dec 2008
Location: Gurgaon, India
Distribution: OpenSUSE 11.4
Posts: 4,581
|
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 Anisha Kaul; 11-10-2010 at 09:35 PM.
Reason: Proper indentation
|
|
|
|
11-08-2010, 11:11 PM
|
#2
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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.
|
11-10-2010, 07:44 AM
|
#3
|
|
Senior Member
Registered: Dec 2008
Location: Gurgaon, India
Distribution: OpenSUSE 11.4
Posts: 4,581
Original Poster
|
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.
|
|
|
|
11-10-2010, 07:26 PM
|
#4
|
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,375
Rep: 
|
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.
|
|
|
|
11-10-2010, 08:05 PM
|
#5
|
|
Senior Member
Registered: Dec 2008
Location: Gurgaon, India
Distribution: OpenSUSE 11.4
Posts: 4,581
Original Poster
|
Thanks graemef,
But I am unable to find any improper indentation 
|
|
|
|
11-10-2010, 08:13 PM
|
#6
|
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,375
Rep: 
|
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 
|
|
|
|
11-10-2010, 09:40 PM
|
#7
|
|
Senior Member
Registered: Dec 2008
Location: Gurgaon, India
Distribution: OpenSUSE 11.4
Posts: 4,581
Original Poster
|
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 Anisha Kaul; 11-10-2010 at 09:41 PM.
|
|
|
|
11-10-2010, 10:05 PM
|
#8
|
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,375
Rep: 
|
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;
}
|
|
|
|
11-10-2010, 10:09 PM
|
#9
|
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,375
Rep: 
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:15 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|