note 34278 added to function.preg-replace

From: Date: Mon, 21 Jul 2003 00:29:58 +0000
Subject: note 34278 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-52436@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34278 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34278&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34278&report=yes

« previous php.notes (#52436) next »