user1860996
user1860996

Reputation: 89

If string contains two characters

I have looked everywhere for something like this. I can't see to get it to work. I'm trying to have a if statement if a string contains a colon AND a dash.

String example (true): "Visor - Color : Black" String example (false): "Water Bottle - Blue"

I have tried different variations of this but its still not working.

if (preg_match(':|-]*', $productName)) {
    TRUE
} else {
    FALSE
}

Any suggestions?

Upvotes: 0

Views: 525

Answers (1)

Federkun
Federkun

Reputation: 36934

Without regex:

if (substr_count($productName, '-') === 1 && substr_count($productName, ':') === 1) {
    // true
}

Upvotes: 1

Related Questions