We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

In over my head: preg_replace_callback() using multiple functions within a plug-in

Development and Programming

Brian M.'s avatar
Brian M.
529 posts
17 years ago
Brian M.'s avatar Brian M.

I’m definitely sinking here - not even treading water.

I’m writing a plug-in that, instead of doing a simple search/replace of provided text, will replace $something with a $begin(ing), an incrementing counter, and an $end(ing). To do this I’m using preg_replace_callback();

It is actually working, but for every loop I’m getting a PHP notice to the tune of:

Notice: Undefined index:

in /path/to/my/plugins/pi.incrementeer.php on line 33

Line 33 is:

$output .= "$begin$counter$end" . $matches[$match];

And the plug-in in it’s entirety:

class Incrementeer {

    var $return_data = "";
    var $counter = 1;

  function Incrementeer() {
    
        global $TMPL;
        $replace = $TMPL->fetch_param('replace');
        $this->return_data = preg_replace_callback("/$replace/", array($this, 'myCallback'), $TMPL->tagdata);
    
  }

    function myCallback($matches)    {

        global $TMPL;
        $begin = $TMPL->fetch_param('begin');
        $end = $TMPL->fetch_param('end');
      $output = '';
    
        foreach($matches as $match) {
                $output .= "$begin$this->counter$end" . $matches[$match];
        $this->counter++;
    }

    return $output;
    }
  
 blah blah usage function

}

Can anyone shed some light here? I’m ok with PHP but am a (relative) newb when it comes to OO programming (self-taught in all the wrong ways). I could cheat and turn off error reporting for the duration of the script, but I don’t want to cheat…

Thank you, thank you, thank you!

       
Brian M.'s avatar
Brian M.
529 posts
17 years ago
Brian M.'s avatar Brian M.

Note that there’s no index even listed in the notice message - that’s not a typo - it’s giving no indication of the missing index.

I’ve been butting my head up against variable scope issues while making this thing work - I’m assuming this is still a problem with scope.

       
Brian M.'s avatar
Brian M.
529 posts
17 years ago
Brian M.'s avatar Brian M.

Just to follow up, I ended up changing the function to use a for loop instead of a foreach loop, and was able to get rid of the offending error notices that way. Back to your regularly scheduled plug-in technical assistance discussions…

       
BlackHelix's avatar
BlackHelix
226 posts
17 years ago
BlackHelix's avatar BlackHelix

What happened is that inside your foreach loop, you were using the “matches” variable. Inside that foreach loop, since you were doing matches as match…. matches is no longer defined INSIDE that loop.

You could have written it like thus:

foreach($matches as $match) {
                $output .= "$begin$this->counter$end" . $match;
        $this->counter++;
    }

The $match variable IS the contents of that array.

I hope that helps! The concept of a foreach loop is a tough one some time.

       
Derek Jones's avatar
Derek Jones
7,561 posts
17 years ago
Derek Jones's avatar Derek Jones

Technically, $matches is still defined; the key however, is not. It’s a difference of:

foreach ($arr as $val)
{
    echo $val;
}

vs.

foreach ($arr as $key => $val)
{
    echo $arr[$key];
}

Unless you use $key => $val pairs in the foreach, the variable available in each loop will be the value itself, not the array’s keys.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.