note 38114 added to function.preg-match-all

From: Date: Mon, 08 Dec 2003 23:51:15 +0000
Subject: note 38114 added to function.preg-match-all
Groups: php.notes 
Request: Send a blank email to php-notes+get-61665@lists.php.net to get a copy of this message
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); } ---- Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+38114 Delete -- http://master.php.net/manage/user-notes.php?action=delete+38114&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+38114&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#61665) next »