Source for file escape.php

Documentation is available at escape.php

  1. <?php
  2.  
  3. /**
  4.  * Applies various escaping schemes on the given string
  5.  * <pre>
  6.  *  * value : the string to process
  7.  *  * format : escaping format to use, valid formats are : html, htmlall, url, urlpathinfo, quotes, hex, hexentity, javascript and mail
  8.  *  * charset : character set to use for the conversion (applies to some formats only), defaults to the current Dwoo charset
  9.  * </pre>
  10.  * This software is provided 'as-is', without any express or implied warranty.
  11.  * In no event will the authors be held liable for any damages arising from the use of this software.
  12.  *
  13.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  14.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  15.  * @license    http://dwoo.org/LICENSE   Modified BSD License
  16.  * @link       http://dwoo.org/
  17.  * @version    1.0.0
  18.  * @date       2008-10-23
  19.  * @package    Dwoo
  20.  */
  21. function Dwoo_Plugin_escape(Dwoo $dwoo, $value='', $format='html', $charset=null)
  22. {
  23.     if ($charset === null) {
  24.         $charset = $dwoo->getCharset();
  25.     }
  26.  
  27.     switch($format)
  28.     {
  29.  
  30.     case 'html':
  31.         return htmlspecialchars((string) $value, ENT_QUOTES, $charset);
  32.     case 'htmlall':
  33.         return htmlentities((string) $value, ENT_QUOTES, $charset);
  34.     case 'url':
  35.         return rawurlencode((string) $value);
  36.     case 'urlpathinfo':
  37.         return str_replace('%2F', '/', rawurlencode((string) $value));
  38.     case 'quotes':
  39.         return preg_replace("#(?<!\\\\)'#", "\\'", (string) $value);
  40.     case 'hex':
  41.         $out = '';
  42.         $cnt = strlen((string) $value);
  43.         for ($i=0; $i < $cnt; $i++) {
  44.             $out .= '%' . bin2hex((string) $value[$i]);
  45.         }
  46.         return $out;
  47.     case 'hexentity':
  48.         $out = '';
  49.         $cnt = strlen((string) $value);
  50.         for ($i=0; $i < $cnt; $i++)
  51.             $out .= '&#x' . bin2hex((string) $value[$i]) . ';';
  52.         return $out;
  53.     case 'javascript':
  54.         return strtr((string) $value, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
  55.     case 'mail':
  56.         return str_replace(array('@', '.'), array('&nbsp;(AT)&nbsp;', '&nbsp;(DOT)&nbsp;'), (string) $value);
  57.     default:
  58.         return $dwoo->triggerError('Escape\'s format argument must be one of : html, htmlall, url, urlpathinfo, hex, hexentity, javascript or mail, "'.$format.'" given.', E_USER_WARNING);
  59.  
  60.     }
  61. }

Documentation generated on Sun, 07 Feb 2010 17:53:42 +0000 by phpDocumentor 1.4.0