Народ, нужна помащь. Есть HTML код: Код (Text): <h1>Lincs:</h1> <br> {repeat:var0} {var0}linc0<br> {/repeat} С помащю регулярки бью его на составные: PHP: <?php $tp_open='{'; $tp_close='}'; preg_match_all("%$tp_open repeat\:(.*)$tp_close(.*)$tp_open/repeat$tp_close%Ui", $this->template[$file_id], $rep, PREG_SET_ORDER); ?> Получаю массив: Код (Text): Array ( [0] => Array ( [0] => { repeat:var0}{var0}linc0<br>{/repeat} [1] => var0 [2] => {var0}linc0<br> ) } 1. Можно ли избавиться от пробела перед repeat? 2. Как дальше обработать блок, чтоб он считал значение переменной из массива?
PHP: <?php $pattern = "%{$tp_open}repeat\:(\w+){$tp_close}(.*){$tp_open}\\/repeat{$tp_close}%is"; preg_match_all($pattern, $str, $rep, PREG_SET_ORDER); а как Вы "считаете значения" переменных?
Э... Пардон, не "считал" а "считывал" из переменной $vars... Чтоб при $vars= array("var0 ","var1 ","var2 "), он бы вывел: HTML: <h1>Lincs:</h1> <br> var0 linc0<br> <br> var1 linc0<br> <br> var2 linc0<br> И можно ли это сделать только с помощю регулярки?
Chaser Покажите, как Вы получаете значения переменных, что есть в linc0 и var0. PS> что var0 - массив, я уже понял, что вы хотите сделать итерацию, я тоже понял. не могу понять как работать с классом. Требуется выводить что-то на экран или просто сделать return Попробуйте еще раз, объяснить что Вам нужно.
Есть класс шаблонизатора: PHP: <?php class template_class { var $tp_open="{"; var $tp_close="}"; var $vars = array(); var $template = array(); function get_tpl($file_id, $tpl_name) { if(empty($tpl_name) || !file_exists($tpl_name)) { return false; } else { $this->template[$file_id] = file_get_contents($tpl_name); } } function set_tpl($file_id, $key, $var) { $this->vars[$file_id][$key] = $var; } function tpl_parse($file_id) { $tp_open=$this->tp_open; $tp_close=$this->tp_close; foreach($this->vars[$file_id] as $find => $replace) { switch(gettype($replace)) { case 'array': $this->template[$file_id] = preg_replace ("/$tp_open$find\[(\w+?)\]$tp_close/ie", '$replace[strtolower (\'\1\')]', $this->template[$file_id]); break; case 'string': $this->template[$file_id] = str_replace($tp_open.$find.$tp_close, $replace, $this->template[$file_id]); break; case 'integer': $this->template[$file_id] = str_replace($tp_open.$find.$tp_close, $replace, $this->template[$file_id]); break; }; } $this->tpl_blocks($file_id); } } ?> Нужно внедрить в него аналог функции foreach, чтоб при орбработки шаблона с кодом: HTML: <h1>Lincs:</h1> <br> {repeat:var0} {var0}linc0<br> {/repeat} Чтоб, при $vars= array("var0 ","var1 ","var2 "), он бы вывел: HTML: <h1>Lincs:</h1> <br> var0 linc0<br> <br> var1 linc0<br> <br> var2 linc0<br> И еще, что тут неверно: PHP: <?php case 'boolean': { if($replace=true) {$this->template[$file_id] = preg_replace ("/$tp_open$find$tp_close(.*)$tp_open\/$find$tp_close/ie", "\1", $this->template[$file_id]);} else {$this->template[$file_id] = preg_replace ("/$tp_open$find$tp_close(.*)$tp_open\/$find$tp_close/ie", "", $this->template[$file_id]);} } break; ?> При обработке строки: HTML: {admin}Vidit toka admin!{/admin} $admin=true
PHP: <?php $this->vars = array( 'var0' => array('mix1', 'mix2', 'mix3') ); preg_match_all(..., $rep, ...); $content = ''; foreach ($this->vars[ $rep[1] ] as $mix) { $content .= str_replace( $rep[1], $mix, $rep[2] ); } var_dump($content); ?>
2topas Подскажи плиз, как можно внедрить это в шаблонизатор =) 2RomanBush: Заменил код на: PHP: <? case 'boolean': $this->template[$file_id] = preg_replace_callback("%$tp_open$find$tp_close(.*)$tp_open\/$find$tp_close%Ui", create_function('$rep','global $replace;if($replace){$return $rep[1];}else{$return "";}'), $this->template[$file_id]); break; ?> Но всеравно не рабтает...
PHP: <? $this->vars = array( 'var0' => array('mix1', 'mix2', 'mix3') ); preg_match_all(..., $rep, ...); $content = ''; foreach ($this->vars[ $rep[1] ] as $mix) { $content .= str_replace( $rep[1], $mix, $rep[2] ); } var_dump($content); ?> Насколько я понел, код работает со всем текстом. А если мне нужно обработать всеволш 1 блок в шаблоне? И еще нащет: PHP: <? case 'boolean': $this->template[$file_id] = preg_replace_callback("%$tp_open$find$tp_close(.*)$tp_open\/$find$tp_close%Ui", create_function('$rep','global $replace;if($replace){$return $rep[1];}else{$return "";}'), $this->template[$file_id]); break; ?> ...
Ну что тут можно сказать... сглупил =) И еще ошибка где-то в самом выражении, а где - не могу понять.
index.php PHP: <?php require('template.class.php'); $parse = new template_class; $parse->get_tpl('home','ht.htm'); $parse->set_tpl('home','admin',true); $parse->tpl_parse('home'); print $parse->template['home'] ?> template.class.php PHP: <?php class template_class { var $tp_open="{"; var $tp_close="}"; var $vars = array(); var $template = array(); function get_tpl($file_id, $tpl_name) { if(empty($tpl_name) || !file_exists($tpl_name)) { return false; } else { $this->template[$file_id] = file_get_contents($tpl_name); } } function set_tpl($file_id, $key, $var) { $this->vars[$file_id][$key] = $var; } function tpl_parse($file_id) { $tp_open=$this->tp_open; $tp_close=$this->tp_close; foreach($this->vars[$file_id] as $find => $replace) { switch(gettype($replace)) { case 'array': $this->template[$file_id] = preg_replace("/$tp_open$find\[(\w+?)\]$tp_close/ie", '$replace[strtolower (\'\1\')]', $this->template[$file_id]); break; case 'string': $this->template[$file_id] = str_replace($tp_open.$find.$tp_close, $replace, $this->template[$file_id]); break; case 'integer': $this->template[$file_id] = str_replace($tp_open.$find.$tp_close, $replace, $this->template[$file_id]); break; case 'boolean': $this->template[$file_id] = preg_replace_callback("%$tp_open$find$tp_close(.*)$tp_open\/$find$tp_close%Uis", create_function('$rep','global $replace; print_r($rep);if($replace){return $rep[1];}else{return "";}'), $this->template[$file_id]); break; } print gettype($replace)."<br>"; } } } ?> ht.htm HTML: {admin}Vidit toka admin!{/admin}<br>
Исправил строчку на: PHP: <?php case 'boolean': $this->template[$file_id] = preg_replace_callback("%$tp_open$find$tp_close(.*)$tp_open\/$find$tp_close%Uis", create_function('$rep',"if($replace){return \$rep[1];}else{return '';};"), $this->template[$file_id]); break; ?> При true все нормально выводит. При false выводит ошибки: Перепроверил все по 5 раз, немагу найти ошибку...
Изменил PHP: <?php case 'boolean': $this->template[$file_id] = preg_replace_callback("%$tp_open$find$tp_close(.*)$tp_open\/$find$tp_close%Uis", create_function('$rep',"if($replace){return \$rep[1];}else{return '';};"), $this->template[$file_id]); break; ?> на PHP: <?php case 'boolean': { if($replace) { $this->template[$file_id] = preg_replace("%$tp_open$find$tp_close(.*)$tp_open\/$find$tp_close%Uis", '\1', $this->template[$file_id]); } else { $this->template[$file_id] = preg_replace("%$tp_open$find$tp_close(.*)$tp_open\/$find$tp_close%Uis", '', $this->template[$file_id]); } break; ?> Ошибки исчезли, но почиму 1 ариант не работает, подскажите?
хотел сделать проверку для email`а, но получается ошибка: Warning: eregi() [function.eregi]: REG_ERANGE in C:\Program Files\Apache Group\Apache2\htdocs\processfeedback.php on line 14 14 line: PHP: if (!eregi('^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$',$email)) пожалуйста, помогите разобраться.