Jump to content

need help with dynamic tags


didgydont

Recommended Posts

Hi all

 

This probarly looks really bad but im trying to replase tags that change. So far it only replaces the last tag and not well either. I will try to show you

function testfunc($value)
  {
       if ($value=='gallery'){$return .= "this gallery has worked = --$value--";}
       if ($value=='email'){$return .= "this email has worked = --$value--";}

    return $return;
  }

$test = "this is a test {%email%} message {%gallery%} rtfgdsfgdsfg";

$test = preg_replace("#(.*){%(.*?)%}(.*)#is",'$1' . testfunc('$2') . '$3', $test);

echo $test;

 

this outputs

this is a test {%email%} message rtfgdsfgdsfg

 

but im trying to get this

this is a test this email has worked = --email-- message this gallery has worked = --gallery-- rtfgdsfgdsfg

 

Im sure im way off but I hope you can understand. Thank you for taking the time to read this post.

 

 

Link to comment
https://forums.phpfreaks.com/topic/256646-need-help-with-dynamic-tags/
Share on other sites

are you after something like...

<?php

function parsetemplate($template, $array) { // Replace template with proper content.

        foreach($array as $a => $b) {
            $template = str_replace("{%{$a}%}", $b, $template);
        }
        return $template;
    }

$test = "this is a test {%email%} message {%gallery%} rtfgdsfgdsfg";

$content['email'] = '[email protected]';
$content['gallery'] = 'random';

echo parsetemplate($test, $content);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.