Jump to content

[SOLVED] matching numbers inside ( )


Michdd

Recommended Posts

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;
?>

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.

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.