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 8223013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:31:12+00:00 2026-06-07T14:31:12+00:00

I have this template file as HTML and willing to replace all matched tags

  • 0

I have this template file as HTML and willing to replace all matched tags eg [**title**] etc with the right content then write to disk as PHP file. I’ve done series of search and none seems to fit my purpose. Below is the HTML code. The problem is it doesn’t always replace the right tag?

<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>[**title**]</title>
  </head>
  <body>
  <!--.wrap-->
  <div id="wrap">
  <!--.banner-->
  <div class="banner">[**banner**]</div>
  <!--/.banner-->

  <div class="list">
    <ul>[**list**]</ul>
  </div>

  <!--.content-->
  <div class="content">[**content**]</div>
  <!--/.content-->

  <!--.footer-->
  <div class="footer">[**footer**]</div>
  <!--/.footer-->

  </div>
  <!--/.wrap-->

</body>
</html>

This is what I have tried so far.

<?php
    $search = array('[**title**]', '[**banner**]', '[**list**]'); // and so on...
    $replace = array(
        'title' => 'Hello World', 
        'list' => '<li>Step 1</li><li>Step 2</li>', // an so on
    ); 

    $template = 'template.html';
    $raw = file_get_contents($template);
    $output = str_replace($search, $replace, $raw);
    $file = 'template.php';
    $file = file_put_contents($file, $output);

?>
  • 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-07T14:31:15+00:00Added an answer on June 7, 2026 at 2:31 pm

    The problem in your code is that you’re using keys in the $replace array. str_replace just replaces based on the position in the array, so the keys do nothing.

    So, you have [**banner**] as the second item in $search, so it would replace it with the second item in replace, which is <li>Step 1</li><li>Step 2</li>.

    If you want to automatically do it by the key (so [**foo**] is always replaced with $replace['foo'], you might want to look at using regular expressions. I knocked up a quick piece of code which worked when I tested it, but there might be bugs:

    <?php
    function replace_callback($matches) {
            $replace = array(
                'title' => 'Hello World', 
                'list' => '<li>Step 1</li><li>Step 2</li>', // an so on
            );
    
        if ( array_key_exists($matches[1], $replace)) {
            return $replace[$matches[1]];
        } else {
            return '';
        }
    }
    
    $template = 'template.html';
    $raw = file_get_contents($template);
    
    $output = preg_replace_callback("/\[\*\*([a-z]+)\*\*\]/", 'replace_callback', $raw);
    
    $file = 'template.php';
    $file = file_put_contents($file, $output);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Absolute beginner question: I have a template file index.html that looks like this: ...
I have javascript code embedded inside a html template file. When I load this
I have this code which will include template.php file from inside each of these
I have an XML Template file. This file contains a header and some predefined
I have simple template that's html mostly and then pulls some stuff out of
I have an HTML file template which is used for mailing purpose here in
SOLVED: Error in template file I have Smarty setup like this: require_once 'smarty/Smarty.class.php'; $smarty
I have a master template file called base.html , in it I have the
Given a yaml file that contains html , like this: template : |+ <div>Hello,
I have a base template file (base.html) and every other template extends to it

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.