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

  • SEARCH
  • Home
  • 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 8609337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:51:45+00:00 2026-06-12T03:51:45+00:00

There are a large numbers of PHP variables I use in my template files

  • 0

There are a large numbers of PHP variables I use in my template files and some of them need to be accessible for editors who can only use HTML. What I want to do exactly is have them identify the variable doing something like [ITEM_NAME_1] when typing/editing content, and PHP finds them because they are wrapped in brackets, and replaces them with the appropriate PHP variable (in that case, $ITEM_NAME_1)

The content of each page is in a variable “$page_content”, so right now the only way I know how to accomplish is to manually write lines to replace what I need.

$page_content = str_replace("[ITEM_NAME_1]", "$ITEM_NAME_1", "$page_content"); 
print $page_content

The issue is there are hundreds of these “ITEM_NAME_X” variables and I don’t want to have to create a massive repeat of “str_replace” lines manually for each ITEM_NAME_X.

What I want to accomplish is have PHP find anything inside $page_content that is wrapped in brackets [], take the name from inside the bracket and replace all of it with the variable of that bracketted name. So if it finds [ITEM_5], it replaces it with $ITEM_5. Is this at all possible? Let me know, thanks.

  • 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-12T03:51:46+00:00Added an answer on June 12, 2026 at 3:51 am

    Give preg_replace with the /e modifier a whirl. Combine that with variable variable name syntax to get the effect you want.

    $page_content = preg_replace('/\[(.*?)\]/e', '$$1', $page_content);
    

    This uses a regex to match strings between square brackets. The strings are captured and are accessed with $1 in the replacement string. The second $ looks up the variable named with whatever’s in $1.

    If you want to do something more complicated with the replacement, you can invoke your own function from the replacement string. For example:

    function substitute_tag($tag_name)
    {
        $replacement_values = array('foo' => 'bar', 'baz' => '42', ...);
        return $replacement_values[$tag_name];
    }
    
    $page_content = preg_replace('/\[(.*?)\]/e', 'substitute_tag("$1")', $page_content);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to use Zend_Filter_Encrypt with large files, without rising memory limit
Is there any way to post large files (>150MB) to Dropbox service with Spring
Is there a input class to deal with [multiple] large XML files based on
How does one paginate a large listing of files within a folder? I can't
CakePHP makes heavy use of associative arrays for passing large numbers of parameters to
I have some large HEX values that I want to display as regular numbers,
I have a php script that outputs a json-encoded object with large numbers (greater
I have a large number of files which I need to backup, problem is
I have a fairly large PHP codebase (10k files) that I work with using
I'm displaying how large a list of files are in a table using PHP.

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.