开发者

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

开发者 https://www.devze.com 2023-02-03 15:59 出处:网络
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 wher

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

精彩评论

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

关注公众号