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
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'] = 'something@somewhere.com';
$content['gallery'] = 'random';

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.