Skip to content
Advertisement

How can I replace a variable in a string with the value in PHP?

I have string like this in database (the actual string contains 100s of word and 10s of variable):

JavaScript

I echo this string like this:

JavaScript

My output is I am a {$club} fan. I want I am a Barcelona fan. How can I do this?

Advertisement

Answer

Use strtr. It will translate parts of a string.

JavaScript

For multiple values (demo):

JavaScript

Program Output:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement