17

Not very sure how to word this question but I'll give an example.

$string = 'Hey, $name. How are you?'; 

When I post this string, $name doesn't change. How can I make it so I can put something like +name+ so it changes to the name. I've tried seaching for it but I don't know what to search for so I don't have any luck. It's probably really simple but i'm just blanking out. Thanks

Tom M
  • 2,815
  • 2
  • 20
  • 47
Alica Kindel
  • 183
  • 1
  • 1
  • 6

5 Answers5

30

You can use place-holders and str_replace. Or use PHP's built-in sprintf and use %s. (And as of v4.0.6 you can swap the ordering of arguments if you'd like).

$name = 'Alica';

// sprintf method:
$format = 'Hello, %s!';
echo sprintf($format, $name); // Hello, Alica!

// replace method:
$format = "Hello, {NAME}!";
echo str_replace("{NAME}", $name, $format);

And, for anyone wondering, I understood that is trouble templating a string, not PHP's integrated concatenation/parsing. I just assume keep this answer up though as I'm still not 100% sure this OP's intent

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • thats what I was looking for, str_replace(); Thanks. I couldn't for the life of me figure it out. – Alica Kindel Jan 03 '12 at 21:37
  • 1
    @Alica: Not a problem. And if this answer was helpful, make sure to accept it (as with the other questions you've asked. Approving previous answers not only gives the person credit, but makes it a resource for future visitors, and makes others want to help you more). – Brad Christie Jan 03 '12 at 21:38
  • `And as of v4.0.6...` I hope you're not mentioning that because you think people are using something lower than PHP 4 ;) –  Jan 03 '12 at 21:39
  • @Tim: You never know. I _hope_ not, but some people like to hold on that antiquated stuff.... – Brad Christie Jan 03 '12 at 21:41
23

I've always been a fan of strtr.

$ php -r 'echo strtr("Hi @name. The weather is @weather.", ["@name" => "Nick", "@weather" => "Sunny"]);'
Hi Nick. The weather is Sunny.

The other advantage to this is you can define different placeholder prefix types. This is how Drupal does it; @ indicates a string to be escaped as safe to output to a web page (to avoid injection attacks). The format_string command loops over your parameters (such as @name and @weather) and if the first character is an @, then it uses check_plain on the value.

Nick
  • 2,803
  • 1
  • 39
  • 59
  • 2
    This is a good answer because it also avoids some pitfalls that str_replace encounters: http://php.net/manual/en/function.strtr.php#117169 – David Duncan Aug 12 '16 at 00:16
10

You should be wrapping the string in double quotes (") if you want variables to be expanded:

$name = "Alica";
$string = "Hey, $name. How are you?"; // Hey, Alica. How are you?

See the documentation.

2

A third solution, no better or worse than the aforementioned, is concatenation:

echo 'Hello '. $name .'!!!';

Josh
  • 12,448
  • 10
  • 74
  • 118
  • It is ok unless you have to support translations where position of the placeholder changes with each language – MTP Apr 27 '23 at 03:53
0

Another way to achieve this is to use msgfmt_format_message

You must have the intl extension enabled. Profile of the method is:

msgfmt_format_message(string $locale, string $pattern, array $values): string|false`

For example:

$name = "Alica";
$string = msgfmt_format_message('en_US', 'Hey, {0}. How are you?', array($name)); 

Would return: Hey, Alica. How are you?

usilo
  • 305
  • 2
  • 10