note 31389 added to function.preg-replace

From: Date: Sat, 19 Apr 2003 23:33:34 +0000
Subject: note 31389 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-46829@lists.php.net to get a copy of this message
I have found that preg_replace doesn't work well in the case of the PEAR Template class. This line in IT.php is doomed: $outer = (0 == count($regs)) ? $this->blocklist[$block] : preg_replace($regs, $values, $this->blocklist[$block]); The reason is that the $values array may contain entries with backslashes and or $ characters. My fix for this is: change: $regs[] = "@" . $this->openingDelimiter . $name . $this->closingDelimiter . "@"; to: $regs[] = $this->openingDelimiter . $name . $this->closingDelimiter; change: // $regs[] = "@".$this->openingDelimiter . $allowedvar . $this->closingDelimiter . "@"; to: $regs[] = $this->openingDelimiter . $allowedvar . $this->closingDelimiter; and change: $outer = (0 == count($regs)) ? $this->blocklist[$block] : preg_replace($regs, $values, $this->blocklist[$block]); to: if (0 == count($regs)) $outer = $this->blocklist[$block]; else { $outer = $this->blocklist[$block]; for ($dk=0; $dk<count($regs); $dk++) { $outer = str_replace($regs[$dk], $values[$dk], $outer); } } This way, I am using str_replace to replace the values instead of preg_replace. Conclusion: preg_replace should not be used when str_replace is what you are trying to do. Visit: http://dave.britiany.com:8081/ to discuss this further. This site is Help You Help Me PHP -- http://www.php.net/manual/en/function.preg-replace.php http://master.php.net/manage/user-notes.php?action=edit+31389 http://master.php.net/manage/user-notes.php?action=delete+31389 http://master.php.net/manage/user-notes.php?action=reject+31389

« previous php.notes (#46829) next »