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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:16:41+00:00 2026-05-15T11:16:41+00:00

Using PHP and/or Codeigniter, I want to create some string templates to be store

  • 0

Using PHP and/or Codeigniter, I want to create some string templates to be store in a database so that later, they can be used to print out a customize statement.

For example, I want to store this "hello %s! you are the %d visitor." in my database so that later I can plug in a name and number to print out this message "hello bob! you are the 100 visitor".

Are there any available functions to easily do this or will I have to write my own script using preg_match/preg_replace?


This works:

<?

$test = 'hello %s';
$name = 'bob';
printf( $test, $name );

?>

Read more: http://php.net/manual/en/function.printf.php

  • 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-05-15T11:16:42+00:00Added an answer on May 15, 2026 at 11:16 am

    you can use sprintf, but you’d always need to know the exact order in which to feed the template its arguments. I’d suggest something more along the lines of

    // this works only on php 5.3 and up
    function sformat($template, array $params)
    {
        return str_replace(
            array_map(
                function($key)
                {
                 return '{'.$key.'}';
                }, array_keys($params)), 
            array_values($params), $template);
    }
    
    // or in the case of a php version < 5.3
    function sformat($template, array $params)
    {
        $output = $template;
        foreach($params as $key => $value)
        {
            $output = str_replace('{'.$key.'}', $value, $output);
        }
        return $output;
    }
    
    
    
    echo sformat('Hello, {what}!', array('what' => 'World'));
    
    // outputs: "Hello, World!"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using CodeIgniter framework for PHP . There are some pages that are
I'm using CodeIgniter and I want to create some sort of filter to know
Using CodeIgniter I am trying to create a link that the user can click
I am on a project using php and codeigniter framework. I want to make
I am using the php framework codeigniter. I am attempting to create a here
I have this code here in PHP using Codeigniter framework, I can't seem to
I am using CodeIgniter. I want to create an array to add to a
I am building a webapp using Codeigniter (PHP) and MongoDB. I am creating indexes
This is weird... I'm using the MVC pattern in php (codeigniter) to display a
I'm using CodeIgniter (a PHP framework) to build an app, and I have an

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.