Disturbed One Posted June 18, 2009 Share Posted June 18, 2009 Hello, If I get a string $str = '(Transaction ID: XXXXXXXXXXXX)'; How can I grab the X values? This is a 12 digit alphanumeric value. I believe this is done using preg_match? But I'm not sure exactly how to use this function. Thank you! Quote Link to comment Share on other sites More sharing options...
flyhoney Posted June 18, 2009 Share Posted June 18, 2009 <?php $string = '(Transaction ID: 1234567653455)'; preg_match('/Transaction ID: ([\d]+)/', $string, $matches); print_r($matches); Quote Link to comment Share on other sites More sharing options...
Disturbed One Posted June 18, 2009 Author Share Posted June 18, 2009 Thanks! That works great, but only if the Transaction ID is numbers only. How can I do it with numbers and letters? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 18, 2009 Share Posted June 18, 2009 preg_match('/Transaction ID: ([0-9a-z]+)/i', $string, $matches); Edit: But if the string is always written like that, it would be more efficient to use substr(): $id = substr($string, 17, 12); Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted June 18, 2009 Share Posted June 18, 2009 <?php $string = '(Transaction ID: 1234567653455)'; preg_match('/Transaction ID: ([\d]+)/', $string, $matches); print_r($matches); Understand that \d is already a shorthand character class (for [0-90-9] exponents in the event your locale supports them), so there would not be any need to encase that inside a character class as you have done. Assuming exponents are not an issue, simply doing (\d+) would have sufficed. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.