This is easily solved with a small bit of regex:
<?php
$str = "Your transaction was successful. Transaction ID: 453712046. Reference code: 1234326. Thank you!";
preg_match_all('#Reference code: (\d+)|Transaction ID: (\d+)#', $str, $matches);
$refCode = $matches[1][1];
$transID = $matches[2][0];
?>
preg_match_all() performs a global regular expression match on the string. Meaning it won't stop after the first match.
Reference code:
and Transaction ID:
will match the literal strings.
\d
Matches a digit ranging from 0-9.
+
Matches between one and unlimited times, as many times as possible, giving back as needed
So this means it will match any number after Reference code: and Reference code: for as long as it's not interupted by a non-digital character.