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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:55:45+00:00 2026-06-08T02:55:45+00:00

I’m trying to run a function whenever there’s a [%xxx%] (acting as a placeholder,

  • 0

I’m trying to run a function whenever there’s a [%xxx%] (acting as a placeholder, if you will), e.g.:

Bla bla bla blabla. Blablabla bla bla.
[%hooray%]
Blabla hey bla bla. [%yay%] blabla bla.

I’m pretty much a PHP beginner, but I managed to crack my head through and come up with the following (yay to me – I somehow managed to understand the basics of regular expressions!):

$maintext = preg_replace("#\[%(.{1,20})%\]#", "display_products('$1')"), $maintext);
echo $maintext; 

I tried working with the e modifier, I tried using preg_replace_callback, and it all does somewhat work – but I don’t need the function to run before I echo the $maintext variable.

Any help on this guys?

edit
The regex isn’t the issue – that’s working fine. I just need to figure out how to run the function only when I echo $maintext… preg_replace_callback runs the function immediately…

  • 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-08T02:55:46+00:00Added an answer on June 8, 2026 at 2:55 am

    Because PHP is a single threaded procedural scripting language1, it doesn’t work in the way you want it to.

    It is not possible to trigger the function to run only when you call echo2, what you need instead is an extra variable.

    $newVariable = preg_replace_callback("#\[%(.{1,20})%\]#", function($matches) {
      // Do your thang here
    }, $maintext);
    
    // $maintext remains the same as it was when you started, do stuff with it here
    
    // When you come to output the data, do...
    echo $newVariable;
    

    There are other approaches to this problem, like wrapping the above code in a function that can be called on demand and using the output buffering callback (see footnote #2), but reading between the lines I think all of this would be overkill for what is a relatively simple problem.

    For the record, I think your regex would be better if you narrow the allowed content down a it, it will potentially match characters you don’t want it to. I suspect that this would be better:

    #\[%(\w{1,20})%\]#
    

    This will allow only the characters a-zA-Z0-9_ in the placeholder.


    1 Others may tell you PHP is now an object oriented language, but they are wrong. It still fundamentally works the same underneath and its still a scripting language, but now provides many OO-features.
    2 It is possible to do something very close to this, but it is very rarely ever a good idea and is far too advanced for dealing with this problem.


    EDIT

    It is important to note that when using either method to pass replacements through a callback (e modifier or preg_replace_callback()) the callback function should return the new string, rather than outputting it directly.

    This is because the code in any statement is executed from the inside out, and the “inner” echo statement (in the callback function) will be executed before the “outer” echo statement (on the line where preg_replace() is called, so the order in which things are output will be incorrect.

    For example:

    $str = "This is my string which contains a %placeholder%";
    
    echo preg_replace_callback('/%(\w+)%/', function($matches) {
      echo "replacement string";
    }, $str);
    // Wrong - outputs "replacement stringThis is my string which contains a "
    
    echo preg_replace_callback('/%(\w+)%/', function($matches) {
      return "replacement string";
    }, $str);
    // Right - outputs "This is my string which contains a replacement string"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I

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.