[prev in list] [next in list] [prev in thread] [next in thread] 

List:       smarty-general
Subject:    [smarty] Extended escape modifier
From:       Mike Krus <mwkrus () yahoo ! com>
Date:       2001-06-28 20:50:03
[Download RAW message or body]

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;|or \
df;|laquo;|not;|shy;|reg;|macr;|deg;|plusmn;|sup2;|sup3;|acute;|micro;|para;|middot;|c \
edil;|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;|Uci \
rc;|Uuml;|Yacute;|THORN;|szlig;|agrave;|aacute;|acirc;|atilde;|auml;|aring;|aelig;|cce \
dil;|egrave;|eacute;|ecirc;|euml;|igrave;|iacute;|icirc;|iuml;|eth;|ntilde;|ograve;|oa \
cute;|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: smarty@progressive-comp.com
To unsubscribe send a blank email to leave-smarty-700385W@lists.ispi.net
Visit Smarty's homepage at http://www.phpinsider.com/php/code/Smarty/


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic