Edit――Sublime快捷键
2015-12-04 12:55
309 查看
<?php
namespace MDPI\SudaBundle\Lib\Email;
/**
* Parse email template from Smarty style to plain text with placeholders.
*/
class Parser
{
/**
* Parse the given text with placeholders
*
* @param string $text
* @param array $placeholders
*
* @return string
*/
public function parse($text, Array $placeholders = array())
{
// we don't want to execute the php code inside the text
$text = str_replace(array('<?', '<?php'), array('<?', '<?php'), $text);
if ($placeholders) {
$topVariables = array();
foreach ($placeholders as $k => $v) {
$topVariables[] = '$' . $k . '=' . var_export($v, true);
}
$text = '<?php ' . join(';', $topVariables) . ' ?>' . $text;
}
// simple logic
// {if expr} > 10} here {else} there {/if}
// {foreach $items as $item} {$item} {/foreach}
$text = preg_replace(
array(
'/\{if\s+([^\}]+?)\}/',
'/\{else\}/',
'/{elseif\s+([^\}]+?)\}/',
'/\{foreach\s+([^\}]+?)\}/',
'/\{\/(if|foreach)\}/'
),
array(
'<?php if($1){ ?>',
'<?php }else{ ?>',
'<?php }else if($1){ ?>',
'<?php foreach($1) { ?>',
'<?php } ?>'
),
$text
);
// replace placeholders
$text = preg_replace_callback(
'/\{\$([a-z][a-z0-9_\'\[\]]+)\}/',
function ($matches) {
return '<?php echo $' . $matches[1] . '; ?>';
},
$text
);
// date tag: {DATE: Y-m-d}
$text = preg_replace_callback("/\{date\s+([^\}]+)\}/", function($matches) {
return date($matches[1]);
}, $text);
$text = str_replace("\t", " ", $text);
$text = str_replace("\n", "\t", $text);
ob_start();
eval(' ?>' . $text . '<?php ');
$text = ob_get_contents();
ob_end_clean();
# It seems the "\n" is removed by the "eval" function.
$text = str_replace("\t", "\n", $text);
return $text;
}
}
namespace MDPI\SudaBundle\Lib\Email;
/**
* Parse email template from Smarty style to plain text with placeholders.
*/
class Parser
{
/**
* Parse the given text with placeholders
*
* @param string $text
* @param array $placeholders
*
* @return string
*/
public function parse($text, Array $placeholders = array())
{
// we don't want to execute the php code inside the text
$text = str_replace(array('<?', '<?php'), array('<?', '<?php'), $text);
if ($placeholders) {
$topVariables = array();
foreach ($placeholders as $k => $v) {
$topVariables[] = '$' . $k . '=' . var_export($v, true);
}
$text = '<?php ' . join(';', $topVariables) . ' ?>' . $text;
}
// simple logic
// {if expr} > 10} here {else} there {/if}
// {foreach $items as $item} {$item} {/foreach}
$text = preg_replace(
array(
'/\{if\s+([^\}]+?)\}/',
'/\{else\}/',
'/{elseif\s+([^\}]+?)\}/',
'/\{foreach\s+([^\}]+?)\}/',
'/\{\/(if|foreach)\}/'
),
array(
'<?php if($1){ ?>',
'<?php }else{ ?>',
'<?php }else if($1){ ?>',
'<?php foreach($1) { ?>',
'<?php } ?>'
),
$text
);
// replace placeholders
$text = preg_replace_callback(
'/\{\$([a-z][a-z0-9_\'\[\]]+)\}/',
function ($matches) {
return '<?php echo $' . $matches[1] . '; ?>';
},
$text
);
// date tag: {DATE: Y-m-d}
$text = preg_replace_callback("/\{date\s+([^\}]+)\}/", function($matches) {
return date($matches[1]);
}, $text);
$text = str_replace("\t", " ", $text);
$text = str_replace("\n", "\t", $text);
ob_start();
eval(' ?>' . $text . '<?php ');
$text = ob_get_contents();
ob_end_clean();
# It seems the "\n" is removed by the "eval" function.
$text = str_replace("\t", "\n", $text);
return $text;
}
}
相关文章推荐
- 允许其他应用程序启动你的Activity
- jsp + tomcat 连接Oracle数据实例
- activity的生命周期
- perl 数组元素为空和数组元素为undef 是两码事
- iOS网络监测方法
- java 试题java编译问题
- UVa 10493 Cats, with or without Hats
- 一铭软件发布新战略迈入共享开源经济时代
- OS 9采用率增速放缓 11月增加4个百分点至70%
- 系统内存管理
- Microsoft Windows 7专业版深圳降价
- Tsinsen-1486:树【Trie树 + 点分治】
- splay初步
- 开源项目XUtils实现多线程下载
- RF+Jenkins构建持续集成
- [软件人生]关于同行竞业,你需要知道,你需要关注-2实例
- Fitbit仍为可穿戴设备龙头老大 苹果屈居第二
- 微软放弃手机版win10 技术支持时间表曝光
- 杭电ACM1250——Hat's Fibonacci
- Java 导入包正确依旧报错的处理方法