1

I'm working to parse the following string but at the same time trying to get rid of the current undefined offset on the index array. I would appreciate some help w/ the undefined offset issue.

error_reporting(E_ALL);
ini_set('display_errors', '1');
function my_recursion($String, &$Inc) {
    $l = strlen($String);
    $has_quotes = 0; $array = array();
    $x= 0;  
    for ($Inc; $Inc < $l; $Inc++) {
        $my_char = $String[$Inc];
        if ($my_char == '(' && !$has_quotes) {
            $Inc++;
            $array[$x] = my_recursion($String, $Inc);
            $x++;
        } else if ($my_char == '"') {
            $has_quotes = !$has_quotes;
            if (!$has_quotes)
                $x++;
        } else if ($has_quotes) {
            $array[$x] .= $my_char;
        }   
    }   
    print_r($array);
}
$String = '(("HELLO"("BAR")("FOO")()""))';
$Inc = 0;
(my_recursion($String, $Inc));
4
  • write variable names in small letters. do not use arrays before defining them array(); Describe what your function is supposed to do when asking a question. Can't reproduce your error here. Commented Mar 19, 2011 at 5:41
  • @tokam: Thanks for the tips, to replicate the notice you have to add: error_reporting(E_ALL); ini_set('display_errors', '1'); Commented Mar 19, 2011 at 5:47
  • I saw the notice, it is because you use the array without defining it first. Commented Mar 19, 2011 at 5:52
  • Can you update your post with what you expect the result to be, please? I'm not sure I understand the intent of the code. Specifically, what do you want in $array once the outermost call returns? Commented Mar 19, 2011 at 6:28

1 Answer 1

1

To get rid of the errors, add this line to the start of your function:

$array = array();

and replace this line:

$array[$x] .= $my_char;

with this:

$array[$x] = isset($array[$x])? $array[$x].$my_char : $my_char;

For help with your recursion, you'll need to describe its desired behavior.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.