2013-04-24 33 views
0

我想在字符串中传递变量,以便稍后在脚本中赋值。想要在php中传递字符串变量中的varialble

$message="hi $(name) your salary $(salary) is credited in your XYZ account"; 
foreach($arrmsgvar as $key => $value){ 
    $temp=array_search($value[1],$upfileformat);   
    if($temp){ 
     $replacement='$row['.$temp.']'; 
     $message=str_replace($value[0],$replacement,$message); 
    } 
} 

我得到串 “喜$行[1]你的薪水$行[2]是记在您的XYZ帐户” 中 $消息

$xdata=""; 
foreach($Spreadsheet as $key => $row){ 
    $xdata.= "`$memid`|`$source`|`$mobile`|`$message`|`0`|`$msgid`||"; 
} 
echo $xdata; 

并获得1 | 2 | 12345678 | hi $row[1] your salary $row[2] is credited in your XYZ account | 0 | 4 ||,in $ xdata

我怎样才能在$ xdata最终输出中获得$ row数组的值?谁会告诉我 一种方法来做到这一点?

回答

0

而不是使用自己的字符串替换,看看使用sprintf

2
$messages = array(); 
$message = "hi %s your salary %d is credited in your %s account"; 
foreach ($arrmsgvar as $key => $value){ 
    $temp = array_search($key, $upfileformat); 
    if ($temp !== false) { 
    $messages[$key] = sprintf($message, $name, $salary, $account); 
    } 
} 
var_dump($messages); 

我不entirly知道在哪里了一些变量从尽管如此, 上述正在添加的代码将会给你如何实现sprintf想法,价值观 $name$salary$account可以被替换为任何文本值应该是。

http://php.net/manual/en/function.sprintf.php

+0

其实我没有$名称,$收入$帐户的值在转让时,如果你看一下我的代码$替换=“$行[”。$温度。“] “; ..我尝试传递只是变量到字符串和后来的值分配给foreach中的变量($ Spreadsheet为$ key => $ row){}在这个循环 – Ekky 2013-04-24 12:43:58