您的位置:首页 > 其它

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: