Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class html_parser
- {
- public static function trim_multichar($str, $chars) {
- foreach (str_split($chars) as $char) {
- $str = trim($str, $char);
- }
- return $str;
- }
- public static function trim_all($array, $chars) {
- foreach ($array as $key => $el) {
- $array[$key] = self::trim_multichar($el, $chars);
- }
- return $array;
- }
- public static function parse_tpl($html, $data = array()) {
- // functions
- $matches = array();
- preg_match('/\s{%(.*?)%}\s/', $html, $matches);
- while (count($matches)) {
- $match = trim($matches[0]);
- $params = self::trim_multichar($match, "{%}% ");
- $params = explode('|', $params);
- $f = trim($params[0]);
- if (function_exists($f)) {
- if (count($params) > 1) {
- unset($params[0]);
- $params = array_merge($params, array());
- }
- $ret = call_user_func_array($f, $params);
- if ($ret !== false) {
- $html = str_replace($match, $ret, $html);
- }
- } else {
- break;
- }
- preg_match('/(\s{%.*?%}\s)/', $html, $matches);
- }
- // variables from $data
- $matches = array();
- preg_match('/{{(.*?)}}/', $html, $matches);
- while (count($matches)) {
- $match = trim($matches[0]);
- $key = self::trim_multichar($match, "{} ");
- $replace = '';
- if (isset($data[$key])) {
- $replace = $data[$key];
- }
- $html = str_replace($match, $replace, $html);
- preg_match('/{{(.*?)}}/', $html, $matches);
- }
- return $html;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment