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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:01:40+00:00 2026-06-13T02:01:40+00:00

I have one array for data $data = array(title=>’some title’, date=>1350498600, story=>’Some story’); I

  • 0

I have one array for data

    $data = array(title=>'some title', date=>1350498600, story=>'Some story');

I have a template

    $template = "#title#, <br>#date(d)#<br> #date(m)#<br>#date(Y)#<br> #story#"; 

All i want is to fit data into template and i know that can be done by str_replace but my problem is the date format. date format is coming from the template not from the data, in data date is stored as php date.
yesterday i tried to ask the same question but i think my question wasn’t clear.
Anybody please help me.

  • 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-13T02:01:41+00:00Added an answer on June 13, 2026 at 2:01 am

    i think it won’t work with str_replace easily so i’m going to use preg_replace

    $data = array('title'=>'some title', 'date'=>1350498600, 'story'=>'Some story');
    $template = "#title#, <br>#date(d)#<br> #date(m)#<br>#date(Y)#<br> #story#"; 
    $result = preg_replace_callback('/#(\w+)(?:\\((.*?)\\))?#/', function ($match) use($data) {
        $value = isset($data[$match[1]]) ? $data[$match[1]] : null;
        
        if (!$value) {
            // undefined variable in template throw exception or something ...
        }
        
        if (! empty($match[2]) && $match[1] == "date") {
            $value = date($match[2], $value);
        }
        
        return $value;
    }, $template);
    

    Instead of using date(m) or date(Y) you could also do things like date(d-m-Y) using this snippet

    This has the disadvantage that you can format only the date variable using this mechanism. But with a few tweaks you can extend this functionality.


    Note: If you use a PHP version below 5.3 you can’t use closures but you can do the following:

    function replace_callback_variables($match) {
        global $data; // this is ugly
    
        // same code as above:
    
        $value = isset($data[$match[1]]) ? $data[$match[1]] : null;
        
        if (!$value) {
            // undefined variable in template throw exception or something ...
        }
            
        if (! empty($match[2]) && $match[1] == "date") {
            $value = date($match[2], $value);
        }
        return $value;
    }
    
    $data = array('title'=>'some title', 'date'=>1350498600, 'story'=>'Some story');
    $template = "#title#, <br>#date(d)#<br> #date(m)#<br>#date(Y)#<br> #story#";
    // pass the function name as string to preg_replace_callback
    $result = preg_replace_callback('/#(\w+)(?:\\((.*?)\\))?#/', 'replace_callback_variables', $template);
    

    You can find more information about callbacks in PHP here

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

Sidebar

Related Questions

I have 2 tables. One (artWork) with all the data I want to pull
I have one main array which is the returned data from a MySQL query
I have converted an array to object data like this: <?php $myobject->data = (object)Array('zero','one','two');
I have one array. I want that array to retain its value between function
I want to implement singleten design pattern in iphone code I have one array.
for example: I have created one javascript property array model with some properties like
I have an HTML Data which looks like: <div id=foo><a class=someClass href=http://somelink>Some Title</a></div> <div
I have an array of data saved in cookie, like this 1:good,2:accelent,3:bad,4:good,fname:Ahmad,lname:Riaz,title:Developer,org:Magiclamp,email:Riaz@khan.com here i
hello all i have one json object like {event1:{title:My birthday,start:12\/27\/2011 10:20 ,end:12\/27\/2011 00:00 },event2:{title:My
I have two arrays of data that I'm trying to amalgamate. One contains actual

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.