Re: Extended escape modifier

From: Date: Thu, 28 Jun 2001 21:24:44 +0000
Subject: Re: Extended escape modifier
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-1077@lists.php.net to get a copy of this message
How does this xml escaping work? Does it just turn a "&" into a "&amp;" if it is followed by something like "foo;" ? If so, there is probably a much simpler way: preg_replace("!&([\w\d]+);!", "&amp;\\1;",$string); Mike Krus wrote: > > 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]+;))%", > "&amp;", $string); > > default: > return $string; > } > } > > I hope the long regex won't get messed by the mailer... > > Mike > > --- > You are currently subscribed to smarty as: monte@ispi.net > To unsubscribe send a blank email to $subst('Email.Unsub') > Visit Smarty's homepage at > http://www.phpinsider.com/php/code/Smarty/ -- Monte Ohrt <monte@ispi.net> http://www.ispi.net/

« previous php.smarty.general (#1077) next »