Source for file Nmtokens.php

Documentation is available at Nmtokens.php

  1. <?php
  2.  
  3. /**
  4.  * Validates contents based on NMTOKENS attribute type.
  5.  * @note The only current use for this is the class attribute in HTML
  6.  * @note Could have some functionality factored out into Nmtoken class
  7.  * @warning We cannot assume this class will be used only for 'class'
  8.  *           attributes. Not sure how to hook in magic behavior, then.
  9.  */
  10. {
  11.     
  12.     public function validate($string$config$context{
  13.         
  14.         $string trim($string);
  15.         
  16.         // early abort: '' and '0' (strings that convert to false) are invalid
  17.         if (!$stringreturn false;
  18.         
  19.         // OPTIMIZABLE!
  20.         // do the preg_match, capture all subpatterns for reformulation
  21.         
  22.         // we don't support U+00A1 and up codepoints or
  23.         // escaping because I don't know how to do that with regexps
  24.         // and plus it would complicate optimization efforts (you never
  25.         // see that anyway).
  26.         $matches array();
  27.         $pattern '/(?:(?<=\s)|\A)'// look behind for space or string start
  28.                    '((?:--|-?[A-Za-z_])[A-Za-z_\-0-9]*)'.
  29.                    '(?:(?=\s)|\z)/'// look ahead for space or string end
  30.         preg_match_all($pattern$string$matches);
  31.         
  32.         if (empty($matches[1])) return false;
  33.         
  34.         // reconstruct string
  35.         $new_string '';
  36.         foreach ($matches[1as $token{
  37.             $new_string .= $token ' ';
  38.         }
  39.         $new_string rtrim($new_string);
  40.         
  41.         return $new_string;
  42.         
  43.     }
  44.     
  45. }

Documentation generated on Thu, 19 Jun 2008 18:49:49 -0400 by phpDocumentor 1.4.2