neha4no
3/6/2019 - 8:53 AM

Tag Content Dynamic - debug + final code

function to convert merge tag into dynamic content based on query param {{get.param}}

  1. checked for regex matched string offset
  2. find char before regex macthed string
  3. find char before regex matched string (string length + offset +1)

function merge_tag_replace_callback($matches) {
    // as usual: $matches[0] is the complete match
    // $matches[1] the match for the first subpattern
    // enclosed in '(...)' and so on

    $match = $matches[1] ? $matches[1] : '';

    return esc_html(isset($_GET[$match]) ? $_GET[$match] : '');
}

/**
 * Use {{get.param_str}} in content and use param_str as query string
 * @param type $content
 * @return type
 */
function merge_tag_content($content) {
  
    echo $content . '<br>';
    preg_match_all('/\{\{get\.([\w\d]*)\}\}/', $content, $matches, PREG_OFFSET_CAPTURE);

    $updated_matched_arr = array();
    // echo $content;
    $matched_arr = $matches[0];
    if (is_array($matched_arr) && count($matched_arr) > 0) {
        foreach ($matched_arr as $val) {
            $reg_str = $val[0];

            $pos = $val[1];
            echo '<br>======START======<br>';
            echo '$reg_str::' . $reg_str . '<br>';
            echo '$pos::' . $pos . '<br>';
            echo 'leng::' . strlen($reg_str) . '<br>';
            $start_str_pos = ($pos - 1);
            $end_str_pos = ($pos + strlen($reg_str));
            $char_before_str = substr($content, $start_str_pos, 1);
            $char_after_str = substr($content, $end_str_pos, 1);
            echo 'before: ' . $start_str_pos . '<br>';
            echo 'after: ' . $end_str_pos . '<br>';
            echo '$before_char: ' . var_dump($char_before_str) . '<br>';
            echo '$after_char: ' . var_dump($char_after_str) . '<br>';
            echo 'content length: ' . strlen($content) . '<br>';
            echo 'str length: ' . $end_str_pos . '<br>';
            echo '============<br>';
            $sub_array = array();
            $sub_array['regex_str'] = $reg_str;
            $sub_array['pos'] = $pos;
            preg_match('/\{\{get\.([\w\d]*)\}\}/', $reg_str, $output_array); 
            $str_to_replace = $output_array[1];
            $replacement = isset($_GET[$str_to_replace]) ? $_GET[$str_to_replace] : '';
            if (strlen($char_before_str) > 0 && $char_before_str != ' ' && !empty($replacement)) {
                $sub_array['space'] = true;
            } else {
                $sub_array['space'] = false;
            }

            $updated_matched_arr[] = $sub_array;
        }
    }

    if (is_array($updated_matched_arr) && count($updated_matched_arr) > 0) {
        foreach ($updated_matched_arr as $value) {
            if ($value['space']) {
                $content = str_replace($value['regex_str'], ' ' . $value['regex_str'], $content);
            }
        }
    }

    return preg_replace_callback("/\{\{get\.([\w\d]*)\}\}/", "merge_tag_replace_callback", $content);
}
Hello{{get.n}}! Lorem ipsum dolor -- {{get.n}}-- sit amet, consectetur adipiscing elit{{get.n}}{{get.job}}

======START======
$reg_str::{{get.n}}
$pos::5
leng::9
before: 4
after: 14
string(1) "o" $before_char: 
string(1) "!" $after_char: 
content length: 106
str length: 14
============

======START======
$reg_str::{{get.n}}
$pos::37
leng::9
before: 36
after: 46
string(1) " " $before_char: 
string(1) "-" $after_char: 
content length: 106
str length: 46
============

======START======
$reg_str::{{get.n}}
$pos::86
leng::9
before: 85
after: 95
string(1) "t" $before_char: 
string(1) "{" $after_char: 
content length: 106
str length: 95
============

======START======
$reg_str::{{get.job}}
$pos::95
leng::11
before: 94
after: 106
string(1) "}" $before_char: 
string(0) "" $after_char: 
content length: 106
str length: 106
============
Hello john! Lorem ipsum dolor -- john-- sit amet, consectetur adipiscing elit john media
<?php
function merge_tag_replace_callback($matches) {
    // as usual: $matches[0] is the complete match
    // $matches[1] the match for the first subpattern
    // enclosed in '(...)' and so on

    $match = $matches[1] ? $matches[1] : '';

    return esc_html(isset($_GET[$match]) ? $_GET[$match] : '');
}

/**
 * Use {{get.param_str}} in content and use param_str as query string
 * @param type $content
 * @return type
 */
function merge_tag_content($content) {
    $regex = '/\{\{get\.([\w\d]*)\}\}/';
    //to get regex_matched string offset
    preg_match_all($regex, $content, $matches, PREG_OFFSET_CAPTURE);

    $updated_matched_arr = array();
  
    $matched_arr = $matches[0];
    if (is_array($matched_arr) && count($matched_arr) > 0) {
        foreach ($matched_arr as $val) {
            $reg_str = $val[0];
            $pos = $val[1];
            
            //check if regex_matched string start position is zero or not, to avoid negative value
            $start_str_pos = ($pos > 0) ? ($pos - 1) : 0; 
            $end_str_pos = ($pos + strlen($reg_str));
            //check if regex_matched string start position is zero or not, to avoid negative value
            $char_before_str = ($pos > 0) ? substr($content, $start_str_pos, 1) : '';
            $char_after_str = substr($content, $end_str_pos, 1);

            $sub_array = array();
            $sub_array['regex_str'] = $reg_str;
            $sub_array['pos'] = $pos;
            
            // check matched string has replacement value or not 
            preg_match($regex, $reg_str, $output_array);
            $str_to_replace = $output_array[1];
            $replacement = isset($_GET[$str_to_replace]) ? $_GET[$str_to_replace] : '';
            // check if there is a char exists before regex_matched_string and is not a blank space and existence of replacement value, if conditions results true then add space before regex_matched_string.
            if (strlen($char_before_str) > 0 && $char_before_str != ' ' && !empty($replacement)) {
                $sub_array['space'] = true;
            } else {
                $sub_array['space'] = false;
            }
            $updated_matched_arr[] = $sub_array;
        }
    }
//$updated_matched_arr conatins regex_match_string, starts position and space flag to add in a main content
    if (is_array($updated_matched_arr) && count($updated_matched_arr) > 0) {
        foreach ($updated_matched_arr as $value) {
            if ($value['space']) {
                $content = str_replace($value['regex_str'], ' ' . $value['regex_str'], $content);
            }
        }
    }
   
    return preg_replace_callback($regex, "merge_tag_replace_callback", $content);
}