<?php
$sql_query = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query .= <<<'EOT'
calculate_tpl = "function normalize($str) {
    $normalize_str = str_replace(array(\"\\r\\n\", \"\\r\", \"\\n\"), \"\\n\", $str);
    return $normalize_str;
}

$result = data_select_array($table_id, 'id=' . $line['id']);
$field_parse = $result['{$field_parse}'];
$begin = normalize('{$give_value_begin}');
$end = normalize('{$give_value_end}');

if ($begin === $end) {
    $res_begin = mb_strpos($field_parse, $begin);
    $temp_field_parse = mb_substr($field_parse, $res_begin + (mb_strlen($begin)));
    $res_end = mb_strpos($temp_field_parse, $end);
    $field_result = mb_substr($temp_field_parse, 0, $res_end + 1 - (mb_strlen($begin)));
} else {
    if (mb_strpos($field_parse, $begin) !== false) {
        $res_begin = mb_strpos($field_parse, $begin);
        if (mb_strpos($field_parse, $end) !== false) {
            $res_end = mb_strpos($field_parse, $end);
            if ($res_begin < $res_end) {
                $end_end = $res_end - $res_begin;
                $field_result = mb_substr($field_parse, $res_begin + ($temp_a_res = ($begin == '\\n') ? 0 : mb_strlen($begin)), $end_end - ($temp_a_res = ($begin == '\\n') ? 0 : mb_strlen($begin)));
            } else {
                $temp_field_parse = mb_substr($field_parse, $res_begin + (mb_strlen($begin)));
                $res_new_end = mb_strpos($temp_field_parse, $end);
                $field_result = mb_substr($temp_field_parse, 0, $res_new_end);
            }
        } else {
            $res_begin = mb_strpos($field_parse, $begin);
            $field_result = mb_substr($field_parse, $res_begin + (mb_strlen($begin)));
        }
    } elseif (mb_strpos($field_parse, $end) !== false) {
        $res_end = mb_strpos($field_parse, $end);
        $field_result = mb_substr($field_parse, 0, $res_end);
    }
}

{if $field_result_type eq 1 || $field_result_type eq 2}
    {if $field_result_type eq 1}
$temp_field_to_int = preg_replace('/[^0-9,]/', '', $field_result);
$temp_field_to_result_int = str_replace(',', '.', $temp_field_to_int);
$line['{$field_result}'] = floatval($temp_field_to_result_int);
    {else}
$line['{$field_result}'] = date('Y-m-d H:i:s', strtotime($field_result));
    {/if}
{else}
$line['{$field_result}'] = trim($field_result);
{/if}" WHERE id = 103
EOT;
run_query($sql_query);
