0

I'm trying to create a class which validates parameters passed into a function, only primitive values though. I've so far done integer, boolean and float. However with my string function, I also want to be able to pass in a parameters about allowed character sets. Like:

  • Allowed upper case
  • Allowed lower case
  • Allowed numbers
  • Allowed special characters

However, I can't figure out how to test this. I've tried doing it with regex, but my best trials aren't working too well.

Any nudge in the right direction would be a great help.

Please note, I'm using an old version for PHP - 5.1.2

Joshua
  • 293
  • 4
  • 19
  • Sorry. Yes, certainly have. I used multiplying prime numbers together for using multiple tags, then using modulo inside function to determine given flag. I've implemented all the min/max size code. Next time I will post what I've attempted. – Joshua Nov 08 '15 at 10:12
  • @AlanMoore This is not a duplicate of the linked question. It has nothing to do with password strength. OP wants to check, if string consists of certain (allowed) characters as I understood the question. – bobble bubble Nov 09 '15 at 16:23
  • @bobblebubble: You're right, I was hasty. But the question is so basic, I don't think it's worth reopening, do you? (No offense intended to the asker; it's not a *bad* question, but it's been asked many times already.) – Alan Moore Nov 09 '15 at 16:49

2 Answers2

3

Depends on what specials. Can try this and use with preg_match.

preg_match('/^[[:alnum:][:punct:]]+$/', $str)

Check out demo at regex101

  • [:alnum:] matches [a-zA-Z0-9]
  • [:punct:] matches [!"#$%&'()*+,\-./:;<=>?@[\\\]^_{|}~] and backtick.
  • + one ore more from ^ start to $ end

See more posix classes

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
-1

Try this:

if(!preg_match('/[.\s]/', $string))
{
  // it contains any single character except dot and white space
  // you can change not permitted list on your demand
}
mahyard
  • 1,230
  • 1
  • 13
  • 34