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! Link to comment https://forums.phpfreaks.com/topic/162813-finding-a-dynamic-string-preg_match/ 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); Link to comment https://forums.phpfreaks.com/topic/162813-finding-a-dynamic-string-preg_match/#findComment-859148 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? Link to comment https://forums.phpfreaks.com/topic/162813-finding-a-dynamic-string-preg_match/#findComment-859155 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); Link to comment https://forums.phpfreaks.com/topic/162813-finding-a-dynamic-string-preg_match/#findComment-859184 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. Link to comment https://forums.phpfreaks.com/topic/162813-finding-a-dynamic-string-preg_match/#findComment-859225 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.