Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

KangarooMusiQue's avatar

Filling a result-string with variables?

I get a text-string from a mySQL-request which contains some variables i want to fill.

The string i get from the mySQL-Table is like "This is something from $username" and i want to fill the "$username" with a value.

As i am very new to programming i get mad searching the web how to fill this variable with any value. If i define $username = "Joe Doe"; this does not fill the $username-Variable witthin the string.

Can someone help?

0 likes
9 replies
MichalOravec's avatar

Something like this

DB::table('your_table')->selectRaw("replace(your_column, '$username', '?')", [$username])->get();
KangarooMusiQue's avatar

Thank you. Can you proide an example how i can replace the variables within the string after i got it from the database? I'm kind of on the edge right now.

Tray2's avatar

If you do this

$user = 'Tray2';

echo "Hello my name is $user"

It will print

Hello my name is Tray2

However comming from the database you need to tell the interpriter to run it as php. That can be done by using eval. However that is really really dangerous.

https://stackoverflow.com/questions/41406/how-do-i-execute-php-that-is-stored-in-a-mysql-database

I would suggest you use sprintf, something like this

$user = 'Tray2';

echo sprintf('Hello my name is %s', $user);

https://www.w3schools.com/php/func_string_sprintf.asp

MichalOravec's avatar

I think I give you an example. If in your database you have stored "This is something from $username" then part of string $username will be replaced with data stored in variable $username.

KangarooMusiQue's avatar

The string i get from the database is "This is my $string" - and i want to change $string within the string to a value i got earlier like $string ="Test". So the result should be "This is my Test". But defining the $string doesnt give me the wanted "replacement" within the string.

MichalOravec's avatar

Here replace Model with your model what you use and field wih your column in database.

$model = Model::first();

$string = "Test";

$model->field = str_replace("$string", $string , $model->field);
KangarooMusiQue's avatar

I have done it like this.

  $results = TextToSpeech::first();
        $title ="Das ist mein Testtitel";
        $newString = str_replace("$title", $title , $results->phrase);
        return $newString;

The result from the database is

This is a testphrase. We are now playing $title

But after the str_replace the result is still:

This is a testphrase. We are now playing $title

I dont know where the problem is :(

KangarooMusiQue's avatar

I have not replaced the text "$title" with "{{title}}" and did:

 $newString = str_replace("{{title}}", $title , $results->phrase);

now it works. seems like the $-char did some messups.

MichalOravec's avatar

@kangaroomusique It was problem with double quotes

$results = TextToSpeech::first();

$title = "Das ist mein Testtitel";

$newString = str_replace('$title', $title , $results->phrase);

return $newString;

But if you can it will be better if you don't save text with normal variable inside, use something different how you mentioned in the last post.

1 like

Please or to participate in this conversation.