2 /** @package verysimple::String */
3 require_once("util/html2text.php");
6 * A set of utility functions for working with simple template files
8 * @package verysimple::String
10 * @copyright 1997-2011 VerySimple, Inc.
11 * @license http://www.gnu.org/licenses/lgpl.html LGPL
17 /** @var used internally for merging. */
18 static $_MERGE_TEMPLATE_VALUES = null;
21 * Transforms HTML into formatted plain text.
25 * @return string plain text
27 static function HtmlToText($html)
29 return convert_html_to_text($html);
33 * Transforms plain text into formatted HTML.
39 static function TextToHtml($txt)
41 // Kills double spaces and spaces inside tags.
42 while (! (strpos($txt, ' ') === false)) {
43 $txt = str_replace(' ', ' ', $txt);
46 $txt = str_replace(' >', '>', $txt);
47 $txt = str_replace('< ', '<', $txt);
49 // Transforms accents in html entities.
50 $txt = htmlentities($txt);
52 // We need some HTML entities back!
53 $txt = str_replace('"', '"', $txt);
54 $txt = str_replace('<', '<', $txt);
55 $txt = str_replace('>', '>', $txt);
56 $txt = str_replace('&', '&', $txt);
58 // Ajdusts links - anything starting with HTTP opens in a new window
59 // $txt = str_ireplace("<a href=\"http://","<a target=\"_blank\" href=\"http://",$txt);
60 // $txt = str_ireplace("<a href=http://","<a target=\"_blank\" href=http://",$txt);
63 $eol = (strpos($txt, "\r") === false) ?
"\n" : "\r\n";
64 $html = '<p>' . str_replace("$eol$eol", "</p><p>", $txt) . '</p>';
65 $html = str_replace("$eol", "<br />\n", $html);
66 $html = str_replace("</p>", "</p>\n\n", $html);
67 $html = str_replace("<p></p>", "<p> </p>", $html);
69 // Wipes <br> after block tags (for when the user includes some html in the text).
80 for ($x = 0; $x < count($wipebr); $x ++
) {
82 $html = str_ireplace("<$tag><br />", "<$tag>", $html);
83 $html = str_ireplace("</$tag><br />", "</$tag>", $html);
90 * Merges data into a template with placeholder variables
91 * (for example "Hello {{NAME}}").
92 * Useful for simple templating
93 * needs such as email, form letters, etc.
95 * If a placeholder is in the template but there is no matching value,
96 * then the placeholder will be left alone and will appear in the output.
98 * Note that there is no escape character so ensure the right and
99 * left delimiters do not appear as normal text within the template
101 * @param string $template
102 * string with placeholder variables
104 * mixed (array or object) $values an associative array or object with key/value pairs
106 * bool true to strip out placeholders with missing value, false to leave them as-is in the output (default true)
108 * string the left (opening) delimiter for placeholders. default = {{
110 * string the right (closing) delimiter for placeholders. default = }}
111 * @return string merged template
113 static function Merge($template, $values, $stripMissingValues = true, $ldelim = "{{", $rdelim = "}}")
115 return $stripMissingValues ? self
::MergeRegEx($template, $values, $ldelim, $rdelim) : self
::MergeSimple($template, $values, $ldelim, $rdelim);
119 * Used internally by Merge, or may be called directly.
120 * If a placeholder is in the template but there is no matching value,
121 * it will be left alone and appear in the template, for example: {{PLACEHOLDER}}.
123 * @param string $template
124 * string with placeholder variables
126 * mixed (array or object) $values an associative array or object with key/value pairs
128 * string the left (opening) delimiter for placeholders. default = {{
130 * string the right (closing) delimiter for placeholders. default = }}
131 * @return string merged template
133 static function MergeSimple($template, $values, $ldelim = "{{", $rdelim = "}}")
135 $replacements = array ();
137 foreach ($values as $key => $val) {
138 $replacements [$ldelim . $key . $rdelim] = $val;
141 return strtr($template, $replacements);
145 * Used internally by Merge, or may be called directly.
146 * If a placeholder is in the template but there is no matching value,
147 * it will be replaced with empty string and will NOT appear in the output.
149 * @param string $template
150 * string with placeholder variables
152 * mixed (array or object) $values an associative array or object with key/value pairs
154 * string the left (opening) delimiter for placeholders. default = {{
156 * string the right (closing) delimiter for placeholders. default = }}
157 * @return string merged template
159 static function MergeRegEx($template, $values, $ldelim = "{{", $rdelim = "}}")
161 self
::$_MERGE_TEMPLATE_VALUES = $values;
163 if ($ldelim != "{{" ||
$rdelim != "}}") {
164 throw new Exception("Custom delimiters are not yet implemented. Sorry!");
167 $results = preg_replace_callback('!\{\{(\w+)\}\}!', 'SimpleTemplate::_MergeRegExCallback', $template);
169 self
::$_MERGE_TEMPLATE_VALUES = null;
175 * called internally by preg_replace_callback
177 * @param array $matches
179 static function _MergeRegExCallback($matches)
181 if (isset(self
::$_MERGE_TEMPLATE_VALUES [$matches [1]])) {
182 return self
::$_MERGE_TEMPLATE_VALUES [$matches [1]];