-3

I have these fields.

$first_name = "first_name_5965_1_5969";

$email = "email_596231_1_596222";

How can I do some regex or only get the "0000_0_0000" part in php? Note that the numbers are always different but the format is the same.

bghouse
  • 478
  • 2
  • 7
  • 22

2 Answers2

1

You might be looking for

\d{4}_\d_\d{4}$

See a demo on regex101.com. That is four digits, a _, another digit, a _, another four digits and the end of the line/string.

Jan
  • 38,539
  • 8
  • 41
  • 69
1

You can use preg_match with the regex for the same.

$email = "email_596231_1_596222";
preg_match('/\d*_\d_\d*$/', $email, $matches);
echo $matches[0];

Output:
596231_1_596222

If you want 0000_0_0000 format i.e. 4 digit in start and 4 digits at end then

preg_match('/\d{4}_\d_\d{4}/', $email, $matches);
echo $matches[0];

Output:
6231_1_5962
Dark Knight
  • 5,128
  • 1
  • 7
  • 27