note 34278 deleted from function.preg-replace by sniper

From: Date: Sun, 07 Sep 2003 21:54:59 +0000
Subject: note 34278 deleted from function.preg-replace by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-55665@lists.php.net to get a copy of this message
Note Submitter: grillen@abendstille.at ---- preg_replace + Objects / Classes there is one problem i experienced: class myText { function replacer($str) { $pattern = '#(<small[^>]*?'.'>)(.*?)(</small>)#si'; return preg_replace($pattern, $this->replace('$2'), $str); } function replace($str) { echo 'infunc: <span class="small">'.$str.'</span>'."\n"; return '<span class="small">'.$str.'</span>'; } } $text = new myText(); $string = 'this is a <small>funny</small> string'; echo 'outfunc: '.$text->replacer($string)."\n"; // returns: // // infunc: <span class="small">$2</span> // outfunc: this is a <span class="small">funny</span> string this $2 which is echoed by the function can make big problems, if you need the word "funny" within this function - e.g. you use switch($str) in the replace-function. i found the following workaround - please tell me if there is a better solution, because i dislike it. if you use $pattern = '#(<small[^>]*?'.'>)(.*?)(</small>)#sie'; return preg_replace($pattern, 'eval(\'return $this->replace("$2");\')', $str); in the replacer-function it returns: // returns: // // infunc: <span class="small">funny</span> // outfunc: this is a <span class="small">funny</span> string hope this helps sb .ma

« previous php.notes (#55665) next »