musoNic80
musoNic80

Reputation: 3696

ereg to preg conversion

I'm a complete novice when it comes to regex. Could someone help me convert the following expression to preg?

ereg('[a-zA-Z0-9]+[[:punct:]]+', $password)

An explanation to accompany any solution would be especially useful!!!!

Upvotes: 0

Views: 1207

Answers (2)

Craig Trader
Craig Trader

Reputation: 15679

To answer your real question, you'd need to structure your code like:

if ( preg_match( '/[a-z]+/', $password ) && 
  preg_match( '/[A-Z]+/', $password ) && 
  preg_match( '/[0-9]+/', $password ) && 
  preg_match( '/[[:punct:]]+/', $password ) ) ...

If you wanted to ensure the presence of at least one lowercase letter, at least one uppercase letter, at least one digit, and at least one punctuation character in your password.

Other questions you should read:

Upvotes: 1

Bob Fincheimer
Bob Fincheimer

Reputation: 18056

preg_match('/[a-zA-Z0-9]+[[:punct:]]+/', $password)

You simply put a / at the beginning and a / at the end. Following the / at the end you can put some different options:

i - case insensitive

g - do a gloabl search

For more information in the beautiful world of regex in PHP, check out this:

http://www.regular-expressions.info/php.html

Upvotes: 1

Related Questions