Escape again
| From: | Mike Krus | Date: | Fri, 20 Jul 2001 22:42:55 +0000 |
| Subject: | Escape again | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-1227@lists.php.net to get a copy of this message | ||
Hi,
1.4.4 came out and I realized I forgot to send the extended
escapes for XML I promised some time back. Here you go:
$smarty_xml_escape_trans = array(
'<' => '<',
'>' => '>'
);
function smarty_mod_escape($string, $esc_type = 'html')
{
global $smarty_xml_escape_trans;
switch ($esc_type) {
case 'html':
return htmlspecialchars($string, ENT_QUOTES);
case 'url':
return urlencode($string);
case 'js':case 'quotes':
// escape unescaped single quotes
return preg_replace("%(?<!\\\\)'%", "\\'", $string);
case 'nl':
return nl2br($string);
case 'xml':
return preg_replace("%&(?!([#\w\d]+);)%", "&", strtr($string, $smarty_xml_escape_trans));
case 'rss':
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]+;))%", "&", strtr($string, $smarty_xml_escape_trans));
default:
return $string;
}
}
"xml" escapes <, > and & which are not part of an entity, even if
that entity is not valid (&foo; will pass unescaped)
"rss" escapes <, > and & which are not part of a valid HTML entity
(&foo; gets escaped to &foo; but é passes unescaped)
hope this helps,
Mike
--
NewsIsFree http://www.newsisfree.com/
We serve 20000+ news feeds each day, for free. Please support us
http://www.paypal.com/xclick/business=donations%40newsisfree.com
Jabber: mkrus@jabber.com ICQ: 30900412