Jump to content

Offset error


EchoFool

Recommended Posts

I am getting an offset error with my string, and am unsure on how to fix it as i don't totally know what is wrong in the first place..... this is what i have:

 

 

<?php
$MainMessage = '[quote] Hey there![/quote]  Hello world!';
//hiiide quote				
$message = explode('[/quote]', $MainMessage);
$quote_times = count($message) - 1;

for($m=0;$m<$quote_times - 1;$m++){
    $lastpart .= $message[$m].'[/quote]';
}
$message[0] = $lastpart.$message[$quote_times - 1].'[/quote]';
$message[1] = $message[$quote_times]; 
?>

 

 

 

The error:

Notice: Undefined offset: -1

 

Notice: Undefined variable: lastpart

 

The "real outcome" should be that:

$message[0] = '[quote] Hey there![/quote]'
$message[1] = 'Hello world!'

 

Not sure why the lastpart variable is not being set?

 

 

I do also have a question, will this script cause an error when ever the $MainMessage has no [ quote] tags. And also will it double crash if there was a message like:

 

$MainMessage = '[quote] [quote] test [/quote] why are you testing[/quote] because i want to!';

Would this still result as:

$message[0] = '[quote] [quote] test [/quote] why are you testing[/quote]'
$message[1] = 'because i want to!'

 

Or it will crash it due to more than one

tag? I have yet to get it working at all but need to know If i am on the right lines?
Link to comment
Share on other sites

I copied your code, initialised the variable $lastpart and placed a couple of var_dumps in there to show the values. Maybe you try that and see how you go. If you are not sure what will happen if you put two

in or whatever, try it and see what output you get. We can tell you, but you will learn more yourself by doing.
Link to comment
Share on other sites

 

1.) $lastpart is unset because you do not define it before using the .= operator. You should define it as an empty string prior to doing this:

 

$lastpart = '';

 

2.) You have an undefined ofset because: count($message) = 1. Therefore, $quote_times = 0. You therefore try to retrieve the index -1 with this part:

 

$message[$quote_times - 1]

 

I assume you only wanted the -1 in one of the lines.

 

3.) Yes. You'll get undefined index errors if $MainMessage has no quote tags. You can check and prevent these by checking the number of parts with count().

 

4.) In your example, you would have an array with 3 elements:  "

test"; "why are you testing"; and "because i want to!"

 

If you wish to have only two parts, take a look into the limit parameter (http://www.php.net/explode)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.