How can I convert ereg expressions to preg in PHP?

The biggest change in the syntax is the addition of delimiters. ereg(‘^hello’, $str); preg_match(‘/^hello/’, $str); Delimiters can be pretty much anything that is not alpha-numeric, a backslash or a whitespace character. The most used are generally ~, / and #. You can also use matching brackets: preg_match(‘[^hello]’, $str); preg_match(‘(^hello)’, $str); preg_match(‘{^hello}’, $str); // etc If … Read more

PHP preg_match with working regex [duplicate]

Why use regex? Use DateTime class. function validateDate($date, $format=”Y-m-d H:i:s”) { $d = DateTime::createFromFormat($format, $date); return $d && $d->format($format) == $date; } You can use this function for all kind of date/time validations. Examples: var_dump(validateDate(‘2012-02-28 12:12:12’)); # true var_dump(validateDate(‘2012-02-30 12:12:12’)); # false var_dump(validateDate(‘2012-02-28’, ‘Y-m-d’)); # true var_dump(validateDate(’28/02/2012′, ‘d/m/Y’)); # true var_dump(validateDate(’30/02/2012′, ‘d/m/Y’)); # false var_dump(validateDate(’14:50′, … Read more