在PHP中提取部分字符串

I have a string (message) and its values change for each transaction. Here is the sample string:

"Your transaction was successful. Transaction ID: 453712046. Reference code: 1234326. Thank you!"

How can I extract the Transaction ID to a variable $transID and the Reference code to the variable $refCode as integer values every time a transaction is made?

Thanks!

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.

This is ugly, and I'm sure there's a better way to do it, but this works:

$str = "Your transaction was successful. Transaction ID: 453712046. Reference code: 1234326. Thank you!" ;

// get the starting and ending of your first string
$pos1 = strpos($str,'Transaction ID: ') + strlen('Transaction ID: ');
$pos2 = strpos($str,'.',$pos1) ;

// get your transaction ID
$transID = substr($str,$pos1, $pos2-$pos1);

// get the starting and ending of your second string
$pos1 = strpos($str,'Reference code: ') + strlen('Reference code: ');
$pos2 = strpos($str,'.',$pos1) ;\

// get your reference code
$refCode = substr($str,$pos1, $pos2-$pos1);

echo $transID . ' - ' . $refCode ;

This is another option, you can do it with several ways. This one is probably the easy to implement and understand, so also maintain.

$str = '"Your transaction was successful. Transaction ID: 453712046. Reference code: 1234326. Thank you!"';
$find = explode(" ",$str);
$Transaction_id = (int) $find[6];
$Reference_code = (int) $find[9];
echo $Transaction_id;
echo $Reference_code;

In order to work, the text must be the same and only change the numbers. ie. "Your transaction was not successful. Transaction ID: 453712046. Reference code: 1234326." wont work.

You could use regex, named preferably to avoid confusion.

some pattern like

/(?<first>Transaction ID: (?<transId>\w{1,})\.)(.*)(?<second>Reference code: (?<refCode>\w{1,})\.)/gm

play arround on this: https://regex101.com/r/fO7ezY/1

<?php
    $re = '/(?<first>Transaction ID: (?<transId>\w{1,})\.)(.*)(?<second>Reference code: (?<refCode>\w{1,})\.)/m';
    $str = 'Your transaction was successful. Transaction ID: 453712046. Reference code: 1234326. Thank you!';

    preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

    // Print the entire match result
    var_dump($matches);
?>

If you are worried about the squence of the transaction ID and reference Code, you could match the pattern twice.