views:

52

answers:

2

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!!!!

+1  A: 
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

Bob Fincheimer
Brilliant, thank you.
musoNic80
+1  A: 

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:

Craig Trader