PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/core/helpers/validation/strings.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 141 lines | 59 code | 15 blank | 67 comment | 16 complexity | 339d300cc0cae16a312f9c08acbec8ab MD5 | raw file
  1. <?php defined('C5_EXECUTE') or die("Access Denied.");
  2. /**
  3. * Functions useful for validating strings
  4. * @package Helpers
  5. * @category Concrete
  6. * @subpackage Validation
  7. * @author Andrew Embler <andrew@concrete5.org>
  8. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  9. * @license http://www.concrete5.org/license/ MIT License
  10. */
  11. class Concrete5_Helper_Validation_Strings {
  12. /**
  13. * Validates an email address
  14. * @param string $address
  15. * @return bool $isvalid
  16. */
  17. public function email($em, $testMXRecord = false) {
  18. if (preg_match('/^([a-zA-Z0-9\._\+-]+)\@((\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,7}|[0-9]{1,3})(\]?))$/', $em, $matches)) {
  19. if ($testMXRecord) {
  20. list($username, $domain) = split("@", $em);
  21. return getmxrr($domain, $mxrecords);
  22. } else {
  23. return true;
  24. }
  25. } else {
  26. return false;
  27. }
  28. }
  29. /**
  30. * Returns true on whether the passed field is completely alpha-numeric
  31. * @param string $field
  32. * @param bool $allow_spaces whether or not spaces are permitted in the field contents
  33. * @param bool $allow_dashes whether or not dashes (-) are permitted in the field contents
  34. * @return bool
  35. */
  36. public function alphanum($field, $allow_spaces = false, $allow_dashes = false) {
  37. if ($allow_spaces && $allow_dashes) {
  38. return !preg_match("/[^A-Za-z0-9 \-]/", $field);
  39. } else if ($allow_spaces) {
  40. return !preg_match("/[^A-Za-z0-9 ]/", $field);
  41. } else if ($allow_dashes) {
  42. return !preg_match("/[^A-Za-z0-9\-]/", $field);
  43. } else {
  44. return !preg_match('/[^A-Za-z0-9]/', $field);
  45. }
  46. }
  47. /**
  48. * Returns true if the passed field is a valid "handle" (e.g. only letters, numbers, or a _ symbol
  49. */
  50. public function handle($handle) {
  51. return !preg_match("/[^A-Za-z0-9\_]/", $handle);
  52. }
  53. /**
  54. * Returns false if the string is empty (including trim())
  55. * @param string $field
  56. * @return bool
  57. */
  58. public function notempty($field) {
  59. return ((is_array($field) && count($field) > 0) || (is_string($field) && trim($field) != ''));
  60. }
  61. /**
  62. * Returns true on whether the passed string is larger or equal to the passed length
  63. * @param string $str
  64. * @param int $length
  65. * @return bool
  66. */
  67. public function min($str, $length) {
  68. return strlen(trim($str)) >= $length;
  69. }
  70. /**
  71. * Returns true on whether the passed is smaller or equal to the passed length
  72. * @param string $str
  73. * @param int $length
  74. * @return bool
  75. */
  76. public function max($str, $length) {
  77. return strlen(trim($str)) <= $length;
  78. }
  79. /**
  80. * Returns 0 if there are no numbers in the string, or returns the number of numbers in the string
  81. * @param string $str
  82. * @return int
  83. */
  84. public function containsNumber($str) {
  85. return strlen(trim(preg_replace('/([^0-9]*)/', '', $str)));
  86. }
  87. /**
  88. * Returns 0 if there are no upper case letters in the string, or returns the number of upper case letters in the string
  89. * @param string $str
  90. * @return int
  91. */
  92. public function containsUpperCase($str) {
  93. return strlen(trim(preg_replace('/([^A-Z]*)/', '', $str)));
  94. }
  95. /**
  96. * Returns 0 if there are no lower case letters in the string, or returns the number of lower case letters in the string
  97. * @param string $str
  98. * @return int
  99. */
  100. public function containsLowerCase($str) {
  101. return strlen(trim(preg_replace('/([^a-z]*)/', '', $str)));
  102. }
  103. /**
  104. * Returns 0 if there are no symbols in the string, or returns the number of symbols in the string
  105. * @param string $str
  106. * @return int
  107. */
  108. public function containsSymbol($str) {
  109. return strlen(trim(preg_replace('/([a-zA-Z0-9]*)/', '', $str))); //we replace a-z and numbers and see if there is anything left.
  110. }
  111. /**
  112. * Returns true if the string contains another string
  113. * @param string $str
  114. * @param array $cont
  115. * @return bool
  116. */
  117. public function containsString($str, $cont = array()) {
  118. $arr = (!is_array($cont)) ? array($cont) : $cont; // turn the string into an array
  119. foreach ($arr as $item) {
  120. if (strstr($str, $item) !== false) {
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. }