Preg_match 2 letters and digits and echo parts of the string?

I am using this regex: (^[A-Z]{2}[0-9]*$) to match similar sting: dadasdasd_ES212121

How do I output:

ES and 212121 separately?

If this how it should be done?

$string = "dadasdasd_ES212121";

if(preg_match('/\_([A-Z]{2}[0-9]*$)/', $string, $matches)){
    echo $matches[0];
    
}

preg_match('/([A-Z]{2})/', $matches[0], $preg_country);
echo $preg_country[0];

preg_match('/([0-9]*$)/', $matches[0], $preg_id);
echo $preg_id[0];
1 Like

Simple example (this site is very good for regex testing)
http://www.phpliveregex.com/p/hLt

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.