开发者

String-manipulation in PHP, insert array keys in user defined string

I have a function that takes the input of a user defined string and an array of data (key=>value), which looks like this;

$text = "Hi! My name is @name, and I live in @location.";
$dataArray = array("name" => "Mikal", "location" => "Oslo, Norway");

function MakeString($text, array $dataArray)
{
// return manipulated string...
}

I would like my function to s开发者_运维技巧wap the string @variables with data from the array, where string-variable matches array-key (if it does), so that the function returns:

"Hi! My name is Mikal, and I live in Oslo, Norway."


    foreach($dataArray as $key=>$value)
    {
     $text= str_replace("@".$key,$value,$text);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜