PHP variable replacement

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

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:

 My name is Joe. 

Update: I am aware of sprintf. But that would not be enough. I changed the code to show what the difference is.

+4
source share
8 answers

it seems strtr is a built-in function that can do the same. (got it from going through drupal code).

 >> $format_str = "My name is %name."; My name is %name. >> strtr($format_str, array('%name' => 'Joe', '%age' => 150)) My name is Joe. 
+10
source

you can 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')); 
+3
source

Do you mean sprintf () ?

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

+2
source

This feature, β€œmap” (and replacement), 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); } 

Great job. Supply any string with variable names in square brackets, and this does the trick.

The syntax is different, but can be changed for your purposes:

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

I prefer it over the% syntax.

0
source

Are you looking for vsprintf . http://us2.php.net/vsprintf

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

0
source

Try giving printf () a whirlwind. There are many examples of using arrays with printf on this website.

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

You can 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 runs into the problem of using extract AND eval while you are in control of the input pattern line, I don't see how this could be a security issue.

-1
source

PHP can extract 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
source

Source: https://habr.com/ru/post/1285941/


All Articles