Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8917037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:23:41+00:00 2026-06-15T05:23:41+00:00

I have a template which has certain fields which are to be replaced with

  • 0

I have a template which has certain fields which are to be replaced with a given value.

The fields each have a name which is enclosed with curly brackets. For instance: {address}
The replacement values are included in an array where the index is the name. For instance array('address'=>'101 Main Street', 'city’=>'New York')

I am using the following, and it works great (most of the time)

$template_new= preg_replace('/\{\?(\w+)\?\}/e', '$array["$1"]', $template);

Problem is if I have a {bad_name} which is not in the array, I get the following error:

Notice: Undefined index: xxx in
/var/www/classes/library.php(860) : regexp code on line
1

My desire is to leave these in place without changing them.

My first thought was to replace $array["$1"] with (isset($array["$1"])?$array["$1"]': '{'.$1.'}'), but it didn’t work.

I also tried try/catch, but it also didn’t help

Please provide any recommendations. Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:23:42+00:00Added an answer on June 15, 2026 at 5:23 am

    You’d have better luck with preg_replace_callback() and doing something like this:

    <?php
    class Substitute {
    
        public function __construct($template) {
            $this->template = $template;
            $this->values = array();
        }
    
        public function run($values) {
            $this->values = $values;
            return preg_replace_callback('/\{\?(\w+)\?\}/', array($this, 'subst'), $this->template);
        }
    
        private function subst($matches) {
            if (isset($this->values[$matches[1]])) {
                return $this->values[$matches[1]];
            }
            // Don't bother doing the substitution.
            return $matches[0];
        }
    }
    

    Keep in mind, I typed that in off the top of my head, so there may be bugs.

    Here’s how you’d do much the same thing with anonymous functions, assuming you’re able to use them:

    function substitute($template, $values) {
        return preg_replace_callback(
            '/\{\?(\w+)\?\}/',
            function ($matches) use ($values) {
                if (isset($values[$matches[1]])) {
                    return $values[$matches[1]];
                }
                // Don't bother doing the substitution.
                return $matches[0];
            },
            $template);
    }
    

    Much more compact!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a velocity template which has a nested table. I want to align
I have an alternate homepage template, homepage.pt, which has a different layout. When I
I have got a CSS division called home which has got certain attributes with
I have already installed and worked template which has gone through many changes as
Problem I have a template container MyContainer<std::unique_ptr<Foo>> which has a std::deque<T> and a std::vector<T>
I have a template which has 3 equally spaced boxes, the problem is that
I have my template file which has data being populated into it. However I
I have a footer template which has a input and submit for email subscription.
I have an email template which has html formatting and place holders to swap
We have a custom weblogic template which has admin server (obviously) and a cluster

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.