PHP:当变量 (b) 包含文本时,如何在另一个变量 (b) 中显示变量 (a) [英] PHP: How to display a variable (a) within another variable(b) when variable (b) contains text

查看:33
本文介绍了PHP:当变量 (b) 包含文本时,如何在另一个变量 (b) 中显示变量 (a)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的数据库中存储文本.以下是正文:

I am storing text in my database. Here is the following text:

".$teamName.", is the name of a recently formed company hoping to take over the lucrative hairdryer design ".$sector."

查询数据库后,我将此文本分配给名为 $news 的变量,然后回显它.

After querying the database I assign this text to a variable called $news, then echo it.

然而,文本输出到屏幕上与上面完全一样,没有将变量 $teamName*$sector 替换为相应的值.

However the text is outputted to the screen exactly as above without the variables $teamName*and $sector replaced by there corresponding values.

我向您保证,$teamName$sector 在我查询数据库之前都已定义.

I assure you that both $teamName and $sector are defined before I query the database.

甚至可以做我想做的事情吗?

Is it even possible to do what I am trying to do?

推荐答案

您最好使用 sprintf() 在这里.

You might be better off using sprintf() here.

$string = "%s is the name of a recently formed company hoping to take over the lucrative hairdryer design %s.";

$teamName = "My Company";
$sector = "sector";

echo sprintf($string, $teamName, $sector);
// My Company is the name of a recently formed company hoping to take over the lucrative hairdryer design sector.

在您的数据库中,您存储 $string.使用 sprintf() 替换变量值.

In your database, you store $string. Use sprintf() to substitute the variable values.

这篇关于PHP:当变量 (b) 包含文本时,如何在另一个变量 (b) 中显示变量 (a)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆