2013-04-24 36 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