Extended escape modifier
| From: | Mike Krus | Date: | Thu, 28 Jun 2001 20:50:03 +0000 |
| Subject: | Extended escape modifier | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-1075@lists.php.net to get a copy of this message | ||
Hi,
I had to extend the behavior of the escape modifier for
my project. Here is the code if someone is interested,
I'd appreciate it got included in future releases
(would spare me updating that tiny part of the code
every new release ;-) )
function smarty_mod_escape($string, $esc_type = 'html')
{
switch ($esc_type) {
case 'html':
return htmlspecialchars($string);
case 'url':
return urlencode($string);
case 'js':// escape unescaped quotes so you can put the string // in '{$string|escape:"js"}'
return preg_replace("%(?<!\\\\)'%", "\\'", $string);
case 'nl':// replace new lines by <BR>
return str_replace("\n", "<BR>", $string);
case 'xml':
// escape entities to produce valid XML...
return preg_replace("%&(?!(nbsp;|iexcl;|cent;|pound;|curren;|yen;|brvbar;|sect;|uml;|copy;|ordf;|laquo;|not;|shy;|reg;|macr;|deg;|plusmn;|sup2;|sup3;|acute;|micro;|para;|middot;|cedil;|sup1;|ordm;|raquo;|frac14;|frac12;|frac34;|iquest;|Agrave;|Aacute;|Acirc;|Atilde;|Auml;|Aring;|AElig;|Ccedil;|Egrave;|Eacute;|Ecirc;|Euml;|Igrave;|Iacute;|Icirc;|Iuml;|ETH;|Ntilde;|Ograve;|Oacute;|Ocirc;|Otilde;|Ouml;|times;|Oslash;|Ugrave;|Uacute;|Ucirc;|Uuml;|Yacute;|THORN;|szlig;|agrave;|aacute;|acirc;|atilde;|auml;|aring;|aelig;|ccedil;|egrave;|eacute;|ecirc;|euml;|igrave;|iacute;|icirc;|iuml;|eth;|ntilde;|ograve;|oacute;|ocirc;|otilde;|ouml;|divide;|oslash;|ugrave;|uacute;|ucirc;|uuml;|yacute;|thorn;|yuml;|lt;|gt;|amp;|quot;|apos;|#[0-9]+;|#[a-fA-F0-9]+;|#o[0-7]+;))%", "&", $string);
default:
return $string;
}
}
I hope the long regex won't get messed by the mailer...
Mike