2

Possible Duplicate:
Converting ereg expressions to preg

I have this ereg() expression:

^[0-9]{8}\T{1}[0-9]{2}\:[0-9]{2}\:[0-9]{2}$

How can I convert it to the preg_match one?

Community
  • 1
  • 1
  • 1
    A general rule for ereg->preg is to simply add a delimiter character to the preg version. e.g. `[abc]` for ereg becomes `/[abc]/` for preg, and then if it was eregi, change it to `/[abc]/i` in preg. – Marc B Jun 23 '11 at 21:13

2 Answers2

3

The expression looks fine, but you can simplify it:

^\d{8}\t\d{2}:\d{2}:\d{2}$

Your original one should still work with preg, but the one above is simpler to read and understand. A few notes:

  • [0-9] is the same as \d
  • {1} is unnecessary
  • : does not need escaping
Chris Laplante
  • 29,338
  • 17
  • 103
  • 134
2

Here you go.

$subject = 'the string i want to search through';
$pattern = '/^[0-9]{8}\T{1}[0-9]{2}\:[0-9]{2}\:[0-9]{2}$/';
$matches = array();
preg_match($pattern, $subject, $matches);

print_r($matches);
Steve Nguyen
  • 5,854
  • 5
  • 21
  • 39