Duncan Palmer Duncan Palmer - 1 year ago 76
PHP Question

Check if a string contains numbers and letters

I want to detect if a string contains both numbers and letters.

For example:

  • Given
    , it would be written to a text file because it contains both.

  • Given
    , it would not, because it only contains letters.

Is there a method to do this?

I was trying to use

$string = PREG_REPLACE("/[^0-9a-zA-Z]/i", '', $buffer);

But it didn't work.

Any help would be appreciated.

Answer Source
if (preg_match('/[A-Z]+[a-z]+[0-9]+/', $myString))
    echo 'Secure enough';

This will ensure us that the string should contain a lower case character as well as an upper case containing a number.

The '+' says that there should be ONE OR MORE character from the type of [A-Z]. As you know, [A-Z] is any character from A to Z and so on

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download