Jump to content

[SOLVED] matching numbers inside ( )


Michdd

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.