1
0

function.library.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * dolphin. Collection of useful PHP skeletons.
  4. * Copyright (C) 2009-2019 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. * @param string $input The string to check
  18. * @param string $mode How the string should be checked
  19. * @param mixed $limit If int given the string is checked for length
  20. *
  21. * @see http://de.php.net/manual/en/regexp.reference.unicode.php
  22. * http://www.sql-und-xml.de/unicode-database/#pc
  23. *
  24. * the pattern replaces all that is allowed. the correct result after
  25. * the replace should be empty, otherwise are there chars which are not
  26. * allowed
  27. *
  28. */
  29. function validate($input,$mode='text',$limit=false) {
  30. // check if we have input
  31. $input = trim($input);
  32. if($input == "") return false;
  33. $ret = false;
  34. switch ($mode) {
  35. case 'mail':
  36. if(filter_var($input,FILTER_VALIDATE_EMAIL) === $input) {
  37. return true;
  38. }
  39. else {
  40. return false;
  41. }
  42. break;
  43. case 'url':
  44. if(filter_var($input,FILTER_VALIDATE_URL) === $input) {
  45. return true;
  46. }
  47. else {
  48. return false;
  49. }
  50. break;
  51. case 'nospace':
  52. // text without any whitespace and special chars
  53. $pattern = '/[\p{L}\p{N}]/u';
  54. break;
  55. case 'nospaceP':
  56. // text without any whitespace and special chars
  57. // but with Punctuation
  58. $pattern = '/[\p{L}\p{N}\p{Po}]/u';
  59. break;
  60. case 'digit':
  61. // only numbers and digit
  62. $pattern = '/[\p{Nd}]/';
  63. break;
  64. case 'pageTitle':
  65. // text with whitespace and without special chars
  66. // but with Punctuation
  67. $pattern = '/[\p{L}\p{N}\p{Po}\p{Z}\s]/u';
  68. break;
  69. case 'text':
  70. default:
  71. $pattern = '/[\p{L}\p{N}\p{P}\p{S}\p{Z}\p{M}\s]/u';
  72. }
  73. $value = preg_replace($pattern, '', $input);
  74. #if($input === $value) {
  75. if($value === "") {
  76. $ret = true;
  77. }
  78. if(!empty($limit)) {
  79. # isset starts with 0
  80. if(isset($input[$limit])) {
  81. # too long
  82. $ret = false;
  83. }
  84. }
  85. return $ret;
  86. }
  87. ?>