如何在另一个区域中使用带有变量名称的 php 变量,就像我自己输入变量名称一样?



下面是我需要帮助的完整代码。代码有注释来帮助进行故障排除和解释我需要什么,但我也会在代码上方进行解释。

问题出在 while 循环内部,或者问题出在 while 循环之外,我不知道,但是当我回显$codemsg时,它不会产生正确的东西。我使$codemsg等于一个带有变量的句子。我需要以不同的方式创建该句子,基于从其他地方引入的变量,例如数据库。该变量是顶部称为$db_message的变量。

我获取该变量,替换字符串的一部分,然后我想在 while 循环中使用它,这就是我不知道该怎么做或如何格式化它的原因。 您可以在我创建变量的部分中看到$outgoing我尝试的不同内容,以及在 while 循环中,我尝试引入我在 while 上方创建的最终$db_message变量的不同方式。

$db_message = "{first} {last}, We will give you up to {value} for your {year} {make} {model}. Visit {url} or call {phone}";
echo "<p>db_message before:<br>".$db_message."</p>";
//convert tags to php variables
$incoming = ["{first}", "{last}", "{value}", "{year}", "{make}", "{model}", "{url}", "{phone}"];
$d = "data";
$dbr0 = "[0]"; $dbr1 = "[1]"; $dbr2 = "[2]"; $dbr3 = "[3]"; $dbr4 = "[4]"; $dbr5 = "[5]"; $dbr6 = "[6]"; $dbr7 = "[7]";
//i don't know if the problem is here, in creating the $outgoing variable
//$outgoing = ["$data[0]", "$data[1]", "$data[2]", "$data[3]", "$data[4]", "$data[5]", "$data[6]", "$data[7]"];
//$outgoing = ["$data[0]", "$data[1]", "$data[2]", "$data[3]", "$data[4]", "$data[5]", "$data[6]", "$data[7]"];
//$outgoing = ["".$data[0]."", "".$data[1]."", "".$data[2]."", "".$data[3]."", "".$data[4]."", "".$data[5]."", "".$data[6]."", "".$data[7].""];
//$outgoing = ["$$d[0]", "$$d[1]", "$$d[2]", "$$d[3]", "$$d[4]", "$$d[5]", "$$d[6]", "$$d[7]"];
$outgoing = ["$$d$dbr0", "$$d$dbr1", "$$d$dbr2", "$$d$dbr3", "$$d$dbr4", "$$d$dbr5", "$$d$dbr6", "$$d$dbr7"];
$db_message = str_replace($incoming, $outgoing, $db_message);
echo "<p>db_message after:<br>".$db_message."</p>";
//csv header line
//first | last | value | year | make | model | url | phone | code
//example records for csv file
//test | me   | 3001 | 2010 | toyota  | camry   | http://testme.some-site.com   | 1-888-888-8888 | 111111
//test | you  | 3002 | 2010 | nissan  | maxima  | http://testyou.some-site.com  | 1-888-888-8888 | 222222
//test | him  | 3003 | 2010 | honda   | civic   | http://testhim.some-site.com  | 1-888-888-8888 | 333333
//test | her  | 3004 | 2010 | hyundai | sonata  | http://testher.some-site.com  | 1-888-888-8887 | 444444
//test | them | 3005 | 2010 | subaru  | legacy  | http://testthem.some-site.com | 1-888-888-8887 | 555555
//test | us   | 3006 | 2010 | acura   | integra | http://testus.some-site.com   | 1-888-888-8887 | 666666
//array built from csv file
$handle = fopen("test.csv", "r");
$row = 0;
$responseMessages = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($data[8] !== "code") { //leave out header line
$code = $data[8];
//this works, but i'm trying to build this from variable up top
//$codemsg = "$data[0] $data[1], We will give you up to $data[2] for your $data[3] $data[4] $data[5]. Visit $data[6] or call $data[7]"; //this works
//$codemsg = $data[0]." ".$data[1].", We will give you up to ".$data[2]." for your ".$data[3]." ".$data[4]." ".$data[5].". Visit ".$data[6]." or call ".$data[7]; //this also works
//trying to make any of the below act like the one above, but none are working
//i don't know if the problem is here, in bringing in the final $db_message variable, after converting tags to variables
$codemsg = "$db_message";
//$codemsg = "".$db_message."";
//$codemsg = $db_message;
//$codemsg = "{$db_message}";
echo $codemsg."<br>";
//this echo right above should be producing lines like this, which is data coming from csv file:
//test me, We will give you up to 3001 for your 2010 toyota camry. Visit http://testme.some-site.com or call 1-888-888-8888
//but instead it's producing lines like this:
//$data[0] $data[1], We will give you up to $data[2] for your $data[3] $data[4] $data[5]. Visit $data[6] or call $data[7]
$responseMessages[$code]['body'] = $codemsg;
$row++;
}
}

您可以在循环中使用str_replace(),而不是尝试"评估"替换。您唯一需要注意的是保持与 CSV 中列的顺序相同的顺序$incoming而不是顺序。

$db_message = "{first} {last}, We will give you up to {value} for your {year} {make} {model}. Visit {url} or call {phone}";
$incoming = ["{first}", "{last}", "{value}", "{year}", "{make}", "{model}", "{url}", "{phone}"];
$handle = fopen("test.csv", "r");
$responseMessages = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($data[8] !== "code") { //leave out header line
$code = $data[8];
$codemsg = str_replace($incoming, $data, $db_message);
echo $codemsg."<br>";
$responseMessages[$code]['body'] = $codemsg;
}
}

输出:

测试你, 我们将给你高达 3002 为您的 2010 日产 maxima.访问 http://testyou.some-site.com 或致电 1-888-888-8888
测试他, 我们将给你高达 3003 为您的 2010 本田思域.访问 http://testhim.some-site.com 或致电 1-888-888-8888
测试她, 我们将给你高达 3004 为您的 2010 现代奏鸣曲.访问 http://testher.some-site.com 或致电 1-888-888-8887
测试它们, 我们将为您的 3005 斯巴鲁遗产提供高达 2010.访问 http://testthem.some-site.com 或致电 1-888-888-8887
测试我们, 我们将为您的 3006 讴歌积分提供高达 2010.访问 http://testus.some-site.com 或致电 1-888-888-8887

你需要使用变量。

看看这里: http://www.dummies.com/programming/php/how-to-use-php-variable-variables/

$Reno = 360000;
$Pasadena = 138000;
$cityname = "Reno";
echo "The size of $cityname is ${$cityname}";
$cityname = "Pasadena";
echo "The size of $cityname is ${$cityname}";

输出将是:

The size of Reno is 360000
The size of Pasadena is 138000

最新更新