Michdd Posted October 19, 2009 Share Posted October 19, 2009 I know I can match numbers by just [0-9]+, so I thought matching numbers inside ( ) would be something like /\([0-9]+\)/ , but that doesn't work Can someone correct me please? Quote Link to comment https://forums.phpfreaks.com/topic/178266-solved-matching-numbers-inside/ Share on other sites More sharing options...
cags Posted October 19, 2009 Share Posted October 19, 2009 Have you got an example pattern that it doesn't work on, because that looks right to me. Quote Link to comment https://forums.phpfreaks.com/topic/178266-solved-matching-numbers-inside/#findComment-939937 Share on other sites More sharing options...
Michdd Posted October 19, 2009 Author Share Posted October 19, 2009 preg_match_all("/\([0-9]+\)/", '(S2O3)-2', $matches); print_r($matches); Output: Array ( [0] => Array ( ) ) Quote Link to comment https://forums.phpfreaks.com/topic/178266-solved-matching-numbers-inside/#findComment-939941 Share on other sites More sharing options...
cags Posted October 19, 2009 Share Posted October 19, 2009 The pattern doesn't match your original specification, you wanted numbers between brackets. What you have is a letter and 3 numbers surrounded by brackets. Therefore it doesn't match. What EXACTLY do you need to match? Quote Link to comment https://forums.phpfreaks.com/topic/178266-solved-matching-numbers-inside/#findComment-939962 Share on other sites More sharing options...
Michdd Posted October 19, 2009 Author Share Posted October 19, 2009 I need to replace (using preg_replace()) all numbers inside of ( ) with <sub>number here</sub> where "number here" is the number matched. Quote Link to comment https://forums.phpfreaks.com/topic/178266-solved-matching-numbers-inside/#findComment-939965 Share on other sites More sharing options...
cags Posted October 19, 2009 Share Posted October 19, 2009 Highly doubt it's the most elegant solution, but how about this.. echo preg_replace("~(\(.*?)(\d+)(.*?\))~", '$1<sub>$2</sub>$3', '(S203)-2'); Quote Link to comment https://forums.phpfreaks.com/topic/178266-solved-matching-numbers-inside/#findComment-939974 Share on other sites More sharing options...
Michdd Posted October 19, 2009 Author Share Posted October 19, 2009 That won't work because the format isn't always non-number, number, non-number. Even in the example I posted it isn't. Quote Link to comment https://forums.phpfreaks.com/topic/178266-solved-matching-numbers-inside/#findComment-939978 Share on other sites More sharing options...
salathe Posted October 19, 2009 Share Posted October 19, 2009 Given the input string "(S2O3)-2", what do you want to be captured (ie, what should/would the matches array look like) exactly? Quote Link to comment https://forums.phpfreaks.com/topic/178266-solved-matching-numbers-inside/#findComment-939985 Share on other sites More sharing options...
thebadbad Posted October 19, 2009 Share Posted October 19, 2009 I would probably go with preg_replace_callback() (with nested preg_replace() calls): <?php $str = '(S2O3)-2'; $str = preg_replace_callback( '~\([^)]*\)~', create_function( '$matches', 'return preg_replace(\'~[0-9]+~\', \'<sub>$0</sub>\', $matches[0]);' ), $str ); echo $str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/178266-solved-matching-numbers-inside/#findComment-939989 Share on other sites More sharing options...
Michdd Posted October 19, 2009 Author Share Posted October 19, 2009 That worked perfectly, thanks. I was gonna use preg_replace_callback() also, but I was going to do it in a different fashion. Quote Link to comment https://forums.phpfreaks.com/topic/178266-solved-matching-numbers-inside/#findComment-939993 Share on other sites More sharing options...
cags Posted October 19, 2009 Share Posted October 19, 2009 Whlist thebadbad's solution is perhaps the better solution, did you even try mine rather disgard it out of hand? Whats the fact it won't always be non-number, number, non-number. Got to do with it, my pattern captures the first bracket and any other character (0 or more meaning there doesn't have to be one) followed by a number, followed by 0 or more characters followed by a closing bracket, it should work fine on your example. Quote Link to comment https://forums.phpfreaks.com/topic/178266-solved-matching-numbers-inside/#findComment-940001 Share on other sites More sharing options...
thebadbad Posted October 19, 2009 Share Posted October 19, 2009 Whlist thebadbad's solution is perhaps the better solution, did you even try mine rather disgard it out of hand? Whats the fact it won't always be non-number, number, non-number. Got to do with it, my pattern captures the first bracket and any other character (0 or more meaning there doesn't have to be one) followed by a number, followed by 0 or more characters followed by a closing bracket, it should work fine on your example. That's a capital o in his sample, not a zero. Easy mistake to make though Quote Link to comment https://forums.phpfreaks.com/topic/178266-solved-matching-numbers-inside/#findComment-940008 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.