28 public static function replace($template, array $data, $isCut =
false)
34 foreach ($data as $key => $value)
36 $key = mb_strtolower($key);
39 $value = self::formatArrayToText($value);
43 $value = (string) $value;
46 $dataTmp[$key] = $value;
50 foreach ($data as $key => $value)
52 $from[] =
'%' . $key .
'%';
56 $template = str_replace($from, $to, $template);
60 $template = self::cut($template, $data);
66 protected static function cut($template, array $data)
71 $matchResult = preg_match_all(
'/\%cut\.([A-Za-z0-9_\.]*)\.(start|end)\%/', $template, $matches, PREG_OFFSET_CAPTURE);
78 foreach ($matches[0] as $key => $match)
82 $var = $matches[1][$key][0];
83 $mod = $matches[2][$key][0];
88 if (!isset($cut[$var]))
90 $cut[$var] = array(
'start' => array(),
'end' => array());
93 if (!isset($cut[$var][$mod]))
100 $pos += strlen($tag);
103 $cut[$var][$mod][] = $pos;
107 foreach ($cut as $key => $item)
110 foreach ($item[
'start'] as $index => $position)
112 if (!isset($item[
'end'][$index]))
117 $sortBy = $item[
'end'][$index];
118 $items[$sortBy] = array(
120 'start' => $item[
'start'][$index],
121 'end' => $item[
'end'][$index]
127 foreach ($items as $item)
129 if (!empty($data[$item[
'key']]))
134 $start = $item[
'start'];
136 if ($start <= 0 || $end <= 0)
141 $template = substr($template, 0, $start) . substr($template, $end);
144 return str_replace($from, $to, $template);