开发者

How to use and parse custom user input variables %var% with PHP?

I want to enter some 开发者_如何学JAVAnews into my website and enter a variable like %custom_heading% and get PHP to replace this with the custom heading I have set, this allows me to enter it in wherever I want, how would I go about doing this?


Update: Thanks this helped a lot :) Already in my code now.


Very simple use str_replace on the string...

$vars = array(
    'name' => 'Joe',
    'age' => 10
);

$str = 'Hello %name% you are %age% years old!';

foreach ($vars as $key => $value) {
    $str = str_replace('%' . $key . '%', $value, $str);
}

echo $str; // Hello Joe you are 10 years old!
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜