如何在html中用2%(%% variable %%)封装的PHP变量中使用 [英] How to use in PHP variables enclosed in double percent(%%variable%%) inside of html
问题描述
我正在修改发送给电子邮件的html文件,我需要通过 PHP 为变量创建一些检查,但我不知道双重百分比是多少 strong>,我尝试搜索网络,但是我看到的结果甚至与编程无关。
我需要确保发送另一封邮件时,因为我只能看到30分钟后发送邮件时输出。
示例代码:
< img style =width:160px; height:auto; width =160src =%% image_url _ {{$ i}} %%alt =%% title _ {{$ i}} %%style =margin:0.5em 1em 0 0/>
不要介意大括号,这些来自Laravel刀片模板,它只是缩短了,我的问题在于那些%% %%。这些数据来自csv。
我需要通过php检查image_url
模板系统简单地替换这些字符串。例如:
//带变量的对象
$ vars = array(
'image_url_1'=> '/images/test.png';
'image_url_2'=>'/images/test2.png';
);
//用tags中的变量转换对象(所以我们不必分析每个项目)
foreach($ vars as& $ value)
$ value =' %%'。 $值。 %%;
$ template ='/templates/test.html.tpl';
echo str_replace(file_get_contents($ template),$ vars);
以上代码将解析 /templates/test.html.tpl
,并将模板中的所有 %%< var> %%
字符串替换为$ vars中的值。
希望这段代码能够为你清除一些事情。
I'm modifying an html file that gets sent to emails, I need to create some checks via PHP to the variables but I don't know what the double percentages are, I tried searching the net but the results I see does not even relate to programming.
I need to be sure when sending another mail since I can only see the output when the mail gets sent 30 mins later.
Sample code:
<img style="width: 160px; height: auto;" width="160" src="%%image_url_{{$i}}%%" alt="%%title_{{$i}}%%" style="margin:0.5em 1em 0 0"/>
Don't mind the curly braces, those are from Laravel blade template which is just a shortened , my problem is those %% %%. The data came from a csv.
I need to check the image_url via php
Template systems simply replace those strings. Example:
// Object with variables
$vars = array(
'image_url_1' => '/images/test.png';
'image_url_2' => '/images/test2.png';
);
// Convert object with variables in "tags" ( so we dont have to parse per item )
foreach ($vars as &$value)
$value = '%%' . $value . '%%';
$template = '/templates/test.html.tpl';
echo str_replace(file_get_contents($template), $vars);
The code above will parse /templates/test.html.tpl
and will replace all %%<var>%%
strings in the template with the values from $vars.
Hope this bit of code clears things up for you.
这篇关于如何在html中用2%(%% variable %%)封装的PHP变量中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!