Ekky
Ekky

Reputation: 790

Want to pass varialble in string variable in php

I want to pass variable in string so that later in script value is assigned to it.

$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);   
    }
 }

I am getting string "hi $row[1] your salary $row[2] is credited in your XYZ account" in $message

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

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

how can i get that value of $row array in $xdata final output? would anyone tell me a way to do this?

Upvotes: 0

Views: 1671

Answers (2)

AlexP
AlexP

Reputation: 9857

$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);

I'm not entirly sure where a number of variables are comming from nevertheless the above code will give you an idea on how to implement sprintf, the values $name,$salary,$account can be replaced with whatever the text values should be.

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

Upvotes: 2

Jimmy Thompson
Jimmy Thompson

Reputation: 1004

Rather than using your own string replace, look at using sprintf.

Upvotes: 0

Related Questions