note 38114 deleted from function.preg-match-all by nlopess

From: Date: Wed, 07 Dec 2005 22:07:41 +0000
Subject: note 38114 deleted from function.preg-match-all by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-99707@lists.php.net to get a copy of this message
Note Submitter: jon at fuck dot org ---- PREG_OFFSET_CAPTURE makes preg_match_all way more useful for template parsing. this function solves the classic template-in-template problem that occurs under certain conditions in other templating systems (fasttemplate). in speed tests i performed using this code in a wrapper class, i found this function to be approximately 500 times faster on it's own than in the class under my host's server conditions. /* * precondition: * TPL_PATH is define()'d, * preferrably with an absolute path. */ function tpl_parse($rel_path, $vars = null) { if (!$vars) return file_get_contents(TPL_PATH . $rel_path); $html = file_get_contents(TPL_PATH . $rel_path); $data = ''; $st = 0; $c = preg_match_all('/\{\$([A-Z_0-9]+)\}/', $html, $m, PREG_PATTERN_ORDER|PREG_OFFSET_CAPTURE); for($x = 0; $x < $c; $x++) { $data .= substr($html, $st, $m[0][$x][1] - $st) . $vars[$m[1][$x][0]]; $st = $m[0][$x][1] + strlen($m[0][$x][0]); } return $data . substr($html, $st); }

« previous php.notes (#99707) next »