4

Is there any function which replaces params in a string? Something like this:

Code:

$format_str = "My name is %name."; /* this was set in a 
                                      configuration file - config.php */

$str = xprintf($format_str, array('name' => 'Joe', 'age' => 150)); 
              /* above is somewhere in main code */

The expected value of $str after the operation is:

My name is Joe.

Update: I am aware of sprintf. But, it would not suffice in this case. I have modified the code to show what is the difference.

1
  • probably an error on the part of the author Commented Jun 9, 2009 at 4:12

8 Answers 8

10

seems like strtr is what is a builtin function which can do the same. (got this from going thru drupal code).

>> $format_str = "My name is %name.";
My name is %name.
>> strtr($format_str, array('%name' => 'Joe', '%age' => 150))
My name is Joe.
Sign up to request clarification or add additional context in comments.

1 Comment

This looks to be exactly what is required.
3

you could use this:

function xprintf($str, $array, $chr = '%') {

   foreach ($array as &$key => $val) {
       $key = $chr . $key;
   }

   return strtr($str, $array);
}

$str = xprintf('My name is %name', array('name' => 'Joe'));

4 Comments

Wtf, why did i get downvoted. I would have talked about sprintf but from the way he asked the question, it seems he wants UNIQUE identifiers not just %s. I get downvoted for reading the question -_-
Similar to your answer, Something more like what he's after: au2.php.net/manual/en/function.sprintf.php#83779 see the example sprintf3
Thx for the link. Ill update my answer with something more like what he wants
The problem with str_replace is that it replaces replacements. For example try this: $str = xprintf('My name is %first %last', array('first' => '%last', 'last' => 'Smith')); You should get 'My name is %last Smith' but instead you get 'My name is Smith Smith'. Using strtr avoids this problem.
2

Do you mean sprintf()?

$str = sprintf("My name is %s.", 'Joe');

Comments

0

This function, "map" (and replace), is part of my web application system:

function replace($search, $replace, $mixed)
{
    if (is_string($mixed)) {
        return @str_replace($search, $replace, $mixed);
    } else if (is_array($mixed)) {
        foreach ($mixed as $k => $v) {
            $mixed[$k] = replace($search, $replace, $v);
        }
        return $mixed;
    } else {
        return $mixed;
    }
}


function map($a, $contents, $case_sensitive=false)
{
    if (!is_array($a)) {
        return $contents;
    }
    if (!$case_sensitive) {
        $a = array_change_key_case($a);
    }
    $s = array();
    $r = array();
    foreach ($a as $k => $v) {
        if (is_scalar($v) || empty($v)) {
            $s[] = "{".$k."}";
            $r[] = $v;
        }
    }
    if (!$case_sensitive) {
        $contents = preg_replace_mixed('/{([-_ =,.\/\'"A-Za-z0-9]+)}/ei', "'{'.strtolower('\\1').'}'", $contents);
    }
    return replace($s, $r, $contents);
}

Does a very good job. Supply any string with bracketed variables names, and it does the trick.

Syntax is different, but could be modified for your purposes:

$str = map(array('name' => 'Joe'), 'My name is {name}');

I prefer it over the % syntax.

Comments

0

You're looking for vsprintf. https://www.php.net/vsprintf

It allows you to pass an array of arguments as you indicated.

Comments

-1

Try giving printf() a whirl. That website has a lot of examples for using arrays with printf.

printf("My name is %s", "Joe"); 

Comments

-1

You could always use eval... (gasp!)

$myStr = 'Hi my name is $name. I\'m $age years old.';
$myVars = array("name" => "Joe", "age" => 6);
$parsed = parseString($myStr, $myVars);

function parseString($str, $vars) {
    extract($vars);
    return eval('return "' . addslashes($str) . '";');
}

Before anyone gets cranky about using extract AND eval, as long as you have control over the input template string, I can't see how this could be a security problem.

Comments

-1

PHP can pull in values from an array without using sprintf():

$data = array('name' => 'Joe', 'age' => 150);
print "My name is $data[name], my age is $data[age].";

1 Comment

Doesn't help - needs to be able to deal with an actual string containing % followed by a word, where the string value is set BEFORE the replacement values are known.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.