', '>', $txt);
$txt = str_replace('< ', '<', $txt);
// Transforms accents in html entities.
$txt = htmlentities($txt);
// We need some HTML entities back!
$txt = str_replace('"', '"', $txt);
$txt = str_replace('<', '<', $txt);
$txt = str_replace('>', '>', $txt);
$txt = str_replace('&', '&', $txt);
// Ajdusts links - anything starting with HTTP opens in a new window
// $txt = str_ireplace("' . str_replace("$eol$eol", "
", $txt) . '
';
$html = str_replace("$eol", "
\n", $html);
$html = str_replace("", "\n\n", $html);
$html = str_replace("", "
", $html);
// Wipes
after block tags (for when the user includes some html in the text).
$wipebr = array (
"table",
"tr",
"td",
"blockquote",
"ul",
"ol",
"li"
);
for ($x = 0; $x < count($wipebr); $x++) {
$tag = $wipebr [$x];
$html = str_ireplace("<$tag>
", "<$tag>", $html);
$html = str_ireplace("$tag>
", "$tag>", $html);
}
return $html;
}
/**
* Merges data into a template with placeholder variables
* (for example "Hello {{NAME}}").
* Useful for simple templating
* needs such as email, form letters, etc.
*
* If a placeholder is in the template but there is no matching value,
* then the placeholder will be left alone and will appear in the output.
*
* Note that there is no escape character so ensure the right and
* left delimiters do not appear as normal text within the template
*
* @param string $template
* string with placeholder variables
* @param
* mixed (array or object) $values an associative array or object with key/value pairs
* @param
* bool true to strip out placeholders with missing value, false to leave them as-is in the output (default true)
* @param
* string the left (opening) delimiter for placeholders. default = {{
* @param
* string the right (closing) delimiter for placeholders. default = }}
* @return string merged template
*/
static function Merge($template, $values, $stripMissingValues = true, $ldelim = "{{", $rdelim = "}}")
{
return $stripMissingValues ? self::MergeRegEx($template, $values, $ldelim, $rdelim) : self::MergeSimple($template, $values, $ldelim, $rdelim);
}
/**
* Used internally by Merge, or may be called directly.
* If a placeholder is in the template but there is no matching value,
* it will be left alone and appear in the template, for example: {{PLACEHOLDER}}.
*
* @param string $template
* string with placeholder variables
* @param
* mixed (array or object) $values an associative array or object with key/value pairs
* @param
* string the left (opening) delimiter for placeholders. default = {{
* @param
* string the right (closing) delimiter for placeholders. default = }}
* @return string merged template
*/
static function MergeSimple($template, $values, $ldelim = "{{", $rdelim = "}}")
{
$replacements = array ();
foreach ($values as $key => $val) {
$replacements [$ldelim . $key . $rdelim] = $val;
}
return strtr($template, $replacements);
}
/**
* Used internally by Merge, or may be called directly.
* If a placeholder is in the template but there is no matching value,
* it will be replaced with empty string and will NOT appear in the output.
*
* @param string $template
* string with placeholder variables
* @param
* mixed (array or object) $values an associative array or object with key/value pairs
* @param
* string the left (opening) delimiter for placeholders. default = {{
* @param
* string the right (closing) delimiter for placeholders. default = }}
* @return string merged template
*/
static function MergeRegEx($template, $values, $ldelim = "{{", $rdelim = "}}")
{
self::$_MERGE_TEMPLATE_VALUES = $values;
if ($ldelim != "{{" || $rdelim != "}}") {
throw new Exception("Custom delimiters are not yet implemented. Sorry!");
}
$results = preg_replace_callback('!\{\{(\w+)\}\}!', 'SimpleTemplate::_MergeRegExCallback', $template);
self::$_MERGE_TEMPLATE_VALUES = null;
return $results;
}
/**
* called internally by preg_replace_callback
*
* @param array $matches
*/
static function _MergeRegExCallback($matches)
{
if (isset(self::$_MERGE_TEMPLATE_VALUES [$matches [1]])) {
return self::$_MERGE_TEMPLATE_VALUES [$matches [1]];
} else {
return "";
}
}
}