validate-input.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * dolphin. Collection of useful PHP skeletons.
  4. * Copyright (C) 2009-2021 Johannes 'Banana' Keßler
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  8. *
  9. * You should have received a copy of the
  10. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  11. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  12. */
  13. /**
  14. * validate the given string with the given type. Optional check the string
  15. * length
  16. *
  17. * @see http://de.php.net/manual/en/regexp.reference.unicode.php
  18. * http://www.sql-und-xml.de/unicode-database/#pc
  19. *
  20. * the pattern replaces all that is allowed. the correct result after
  21. * the replace should be empty, otherwise are there chars which are not
  22. * allowed
  23. *
  24. * @param string $input The string to check
  25. * @param string $mode How the string should be checked
  26. * @param mixed $limit If int given the string is checked for length
  27. *
  28. * @return bool
  29. */
  30. function validate($input,$mode='text',$limit=false) {
  31. // check if we have input
  32. $input = trim($input);
  33. if($input == "") return false;
  34. $ret = false;
  35. switch ($mode) {
  36. case 'mail':
  37. if(filter_var($input,FILTER_VALIDATE_EMAIL) === $input) {
  38. return true;
  39. }
  40. else {
  41. return false;
  42. }
  43. break;
  44. case 'url':
  45. if(filter_var($input,FILTER_VALIDATE_URL) === $input) {
  46. return true;
  47. }
  48. else {
  49. return false;
  50. }
  51. break;
  52. case 'nospace':
  53. // text without any whitespace and special chars
  54. $pattern = '/[\p{L}\p{N}]/u';
  55. break;
  56. case 'nospaceP':
  57. // text without any whitespace and special chars
  58. // but with Punctuation
  59. $pattern = '/[\p{L}\p{N}\p{Po}]/u';
  60. break;
  61. case 'digit':
  62. // only numbers and digit
  63. $pattern = '/[\p{N}]/u';
  64. break;
  65. case 'pageTitle':
  66. // text with whitespace and without special chars
  67. // but with Punctuation
  68. $pattern = '/[\p{L}\p{N}\p{Po}\p{Z}\s]/u';
  69. break;
  70. # strange. the \p{M} is needed.. don't know why..
  71. case 'filename':
  72. $pattern = '/[\p{L}\p{N}\p{M}\-_\.\p{Zs}]/u';
  73. break;
  74. case 'text':
  75. default:
  76. $pattern = '/[\p{L}\p{N}\p{P}\p{S}\p{Z}\p{M}\s]/u';
  77. }
  78. $value = preg_replace($pattern, '', $input);
  79. #if($input === $value) {
  80. if($value === "") {
  81. $ret = true;
  82. }
  83. if(!empty($limit)) {
  84. # isset starts with 0
  85. if(isset($input[$limit])) {
  86. # too long
  87. $ret = false;
  88. }
  89. }
  90. return $ret;
  91. }
  92. ?>