PHP:当文本可能是多种可能性之一时,在文本中显示变量的最有效方法



下面是我最初问题的链接:

PHP:当变量(b)包含文本 时,如何在另一个变量(b)中显示变量(a)

好吧,这里还有更多的问题,你的建议都有效,但现在我正在寻找最有效的方法来解决我的具体问题。

在我的数据库中,我有几个文本块。当用户(描述为$teamName)登录到站点时,他们被随机分配这些文本块中的一个。每个文本块都是不同的,并且可能包含不同的变量。

问题是我不知道哪块文本被分配给用户,而没有实际查看数据库或运行查询。因此,目前我必须查询数据库并选择与用户已分配的文本块对应的$newsID

因为我已经预设了文本块,我知道它们包含什么,所以我可以知道switch($newsID),并根据$newsID的值,然后运行插入sprintf()函数的正确值。

然而,有很多文本块,所以会有很多case "":break;的实例。我希望有网站的工作,以便如果在任何阶段我改变文本块不同的东西,然后sprintf()内的变量自动更新,而不是我手动更新switch() case:内的sprintf()

很抱歉写了这么长一篇,希望你能理解。

编辑:我有这些预先确定的文本块在我的数据库在我的teamNews表:

For $newsID = 1:

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

For $newsID = 2:

"The government is excited about the potential of ".$teamName.", after they made an annoucement that they have hired $HoM"

For $newsID = 3:

"It is rumored that $teamName are valuing their hairdryer at $salePrice. People are getting excited.

当用户($teamName)登录游戏时,他们会被随机分配$newsID为1、2或3的文本块之一。假设用户被分配了$newsID = 2的文本块。因此,现在他们的用户名($teamName)被插入到数据库中与他们选择的文本相同的行中。

现在我想显示这个用户对应的文本,所以我做了下面的操作:

$news = news ($currentStage,$teamName);
switch ($ID)
{
    case "1":
    sprintf($teamName,$sector)
    echo $news."<br/><hr/>";    
    break;

    case "2":
    sprintf($teamName,$Hom)
    break;
    case "3":
        sprintf($teamName,$saleprice)
    break;
}

$currentStage--;
}

具有

功能
function news($period,$teamName)
{

$news = mysql_query("
    SELECT `content`,`newsID` FROM `teamnews` WHERE `period` = '$period' && `teamName` = '$teamName'
    ") or die($news."<br/><br/>".mysql_error());

    $row = mysql_fetch_assoc($news);
    $news = $row['content'];
    $ID = $row ['newsID']; 
    return $news,$ID;
}

问题是,在现实中,大约有20个不同的文本块可以分配给用户。所以我会有很多case:。此外,如果我想更改数据库中的所有文本块,我还必须手动更改sprintf 's中的所有变量:'

我想知道是否有更好的方法来做到这一点,以便如果我改变数据库中的文本,那么传递给sprintf的参数将相应地改变。

所以如果我使用

$replaces = array(
 'teamName' => 'Bob the team',
 'sector' => 'murdering',
 'anotherSector' => 'giving fluffy bunnies to children'
);

是否可以这样做:

$replaces = array(
 '$teamName' => '$teamName',
 '$sector' => '$sector',
 '$anotherSector' => '$anothersector'
);

我建议您已经固定了一组命名占位符,并使用str_replace()eval()(邪恶)替代方法。

所以你会(例如)总是有一个$teamName$sector -你可能只是有时使用$anotherSector。这里有两个字符串

1 - $teamName, is the name of a recently formed company hoping to take over the lucrative $sector.
2 - The people at $teamName hate working in $sector, they would much rather work in $anotherSector

如果你要做:

$replaces = array(
  '$teamName' => 'Bob the team',
  '$sector' => 'murdering',
  '$anotherSector' => 'giving fluffy bunnies to children'
);
$news = str_replace(array_keys($replaces),array_values($replaces),$news);

会得到

1 - Bob the team, is the name of a recently formed company hoping to take over the lucrative murdering.
2 - The people at Bob the team hate working in murdering, they would much rather work in giving fluffy bunnies to children

只要你的占位符有已知的名字,它们不必都出现在字符串中——只有相关的才会被替换。

您可以创建一个简单的模板语言,并将模板存储在数据库中。您可以使用strtr

function replaceTemplateVars($str, $data) {
    // change the key format to correspond to the template replacement format
    $replacepairs = array();
    foreach($data as $key => $value) {
        $replacepairs["{{{$key}}}"] = $value;
    }
    // do the replacement in bulk
    return strtr($str, $replacepairs);
}
// store your teamNews table text in this format
// double curly braces is easier to spot and less ambiguous to parse than `$name`.
$exampletemplate = '{{teamName}} is {{sector}} the {{otherteam}}!!'
// get $values out of your database for the user
$values = array(
    'teamName' => 'Bob the team',
    'sector' => 'murdering',
    'otherteam' => 'fluffy bunnies'
);
echo replaceTemplateVars($exampletemplate, $values);
// this will echo "Bob the team is murdering the fluffy bunnies!!"

如果你有更大的需求,比如循环或过滤器,你应该找一个第三方php模板语言并使用它。

函数eval呢?
http://php.net/eval

最新更新