vendredi 21 octobre 2016

PHP - Check if string has a certain character in it, followed by numbers

I have a bunch of 4-character strings that look like this:

"6x66"
"x640"
"et-2"
"32_n"
"6x66"
"anco"
"x150"

What I need to do is check if the string has an "x" in it, followed by either 2 or 3 numbers. If the string has an "x" but no numbers it doesn't qualify, only strings which contain the letter "x" AND have 2 or 3 numbers after that qualify. I am certain it's to be done with a regex pattern but I just can't figure it out on my own. From the top pattern it would be like this.

"6x66" = true
"x640" = true
"xt-2" = false
"32_n" = false
"6x66" = true
"anco" = false
"x150" = true

Aucun commentaire:

Enregistrer un commentaire