Roma Kap
Roma Kap

Reputation: 636

preg_match multiple pattern

Previous questions explain some issues like this, but I couldn't implement solution for my issue.

I tried to create pattern for: 1. Big Character 2. Small Character 3. Number 4. SpecialChar

All this conditions must contain my string. I can check conditions 1,2,3 with:

[0-9A-Za-z]

I can check condition number 4 (special chars) with:

[[:punct:]]

But I can't get this combination of them to work:

$p = "aAbB4#"; //correct string

if(!preg_match('/([0-9A-Za-z]{4,50}|[[:punct:]])/',$p)){
      $p = "Not all credentinals are correct";
}

How could I do that?

Upvotes: 1

Views: 1303

Answers (1)

Zbynek Vyskovsky - kvr000
Zbynek Vyskovsky - kvr000

Reputation: 18865

Use positive look-ahead matching for this, i.e. put every character into (?=) group:

if (!preg_match('/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[[:punct:]]).{4,50}$/', $p)) {
    ...
}

Upvotes: 1

Related Questions