Regex.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Validates values using regular expressions
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.01 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category HTML
  15. * @package HTML_QuickForm
  16. * @author Bertrand Mansion <bmansion@mamasam.com>
  17. * @copyright 2001-2009 The PHP Group
  18. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  19. * @version CVS: $Id: Regex.php,v 1.6 2009/04/04 21:34:04 avb Exp $
  20. * @link http://pear.php.net/package/HTML_QuickForm
  21. */
  22. /**
  23. * Validates values using regular expressions
  24. *
  25. * @category HTML
  26. * @package HTML_QuickForm
  27. * @author Bertrand Mansion <bmansion@mamasam.com>
  28. * @version Release: 3.2.11
  29. * @since 3.2
  30. */
  31. class HTML_QuickForm_Rule_Regex extends HTML_QuickForm_Rule
  32. {
  33. /**
  34. * Array of regular expressions
  35. *
  36. * Array is in the format:
  37. * $_data['rulename'] = 'pattern';
  38. *
  39. * @var array
  40. * @access private
  41. */
  42. var $_data = array(
  43. 'lettersonly' => '/^[a-zA-Z]+$/',
  44. 'alphanumeric' => '/^[a-zA-Z0-9]+$/',
  45. 'numeric' => '/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/',
  46. 'nopunctuation' => '/^[^().\/\*\^\?#!@$%+=,\"\'><~\[\]{}]+$/',
  47. 'nonzero' => '/^-?[1-9][0-9]*/'
  48. );
  49. /**
  50. * Validates a value using a regular expression
  51. *
  52. * @param string $value Value to be checked
  53. * @param string $regex Regular expression
  54. * @access public
  55. * @return boolean true if value is valid
  56. */
  57. function validate($value, $regex = null)
  58. {
  59. // Fix for bug #10799: add 'D' modifier to regex
  60. if (isset($this->_data[$this->name])) {
  61. if (!preg_match($this->_data[$this->name] . 'D', $value)) {
  62. return false;
  63. }
  64. } else {
  65. if (!preg_match($regex . 'D', $value)) {
  66. return false;
  67. }
  68. }
  69. return true;
  70. } // end func validate
  71. /**
  72. * Adds new regular expressions to the list
  73. *
  74. * @param string $name Name of rule
  75. * @param string $pattern Regular expression pattern
  76. * @access public
  77. */
  78. function addData($name, $pattern)
  79. {
  80. $this->_data[$name] = $pattern;
  81. } // end func addData
  82. function getValidationScript($options = null)
  83. {
  84. $regex = isset($this->_data[$this->name]) ? $this->_data[$this->name] : $options;
  85. // bug #12376, converting unicode escapes and stripping 'u' modifier
  86. if ($pos = strpos($regex, 'u', strrpos($regex, '/'))) {
  87. $regex = substr($regex, 0, $pos) . substr($regex, $pos + 1);
  88. $regex = preg_replace('/(?<!\\\\)(?>\\\\\\\\)*\\\\x{([a-fA-F0-9]+)}/', '\\u$1', $regex);
  89. }
  90. return array(" var regex = " . $regex . ";\n", "{jsVar} != '' && !regex.test({jsVar})");
  91. } // end func getValidationScript
  92. } // end class HTML_QuickForm_Rule_Regex
  93. ?>