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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:59:22+00:00 2026-06-08T18:59:22+00:00

I am looking for a solution where I can use preg_replace to replace certain

  • 0

I am looking for a solution where I can use preg_replace to replace certain strings in a body of text where some of the strings may contain a variable which will then call upon a database to retrieve the replacement value (or a function which retrieves that value).

For example, I may want to replace the following strings as follows – the one in italics is the one I am struggling with:

{today} => today's date

{title} => document title

{lang:12} => from "lang" table in database where id=12

The first two are easy, obviously, but I am struggling with the third. The only thing I can think of is to do the replacements of all those that don’t have a colon and then any which do have a colon, to trigger a function.

  • 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-08T18:59:23+00:00Added an answer on June 8, 2026 at 6:59 pm

    You can trigger a function using this :

    $s = preg_replace_callback('/\{lang:([0-9]+)\}/', function($m) {
      $id = $m[1];
      // Mysql query
      return $string_from_database;
    }, $s);
    

    Even better, with required strings caching so that they won’t be fetched several times from database :

    if(preg_match_all('/\{lang:([0-9]+)\}/', $text, $m)) {
        $ids = array_unique(array_filter($m[1], function($id) {
            return (int)$id;
        }));
        $langs = array();
        if($r = mysql_query('SELECT id, txt FROM Lang WHERE id IN('.implode(', ', $ids).')')) {
            while($l = mysql_fetch_assoc($r)) $langs[$l['id']] = $l['txt'];
        }
        $text = preg_replace_callback('/\{lang:([0-9]+)\}/', function($m) use($langs) {
            $id = $m[1];
            return array_key_exists($id, $langs) ? $langs[$id] : '!!! Lang not found !!!';
        }, $text);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a caching solution for java which can support query cache/indexing.
Can anyone find a more Python'ic, more beautiful solution? I’m looping through some text
I'm looking for a solution which can be used to reboot a rooted device.
Could someone please tell me how I can use find_by_attribute with a variable which
I'm looking for a pre-built solution I can use in my RoR application. I'm
I'm looking for a solution, where users of my site can use an image
I am looking for a solution\software that I can use in order to render
I'm looking for solution how can run Ruby script without invoking it directly like
im looking forward for a 3rd part tool/solution that can monitor my server's network
I've been spending hours looking for a solution on this problem. As you can

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.