Jump to content

How to split these up???


Scooby08

Recommended Posts

I'm trying to figure out a way to split up these lines:

 

+7.5-110

-7.5-110

+7.5+110

-7.5+110

 

Shooting for a way to get this:

 

+7.5 (-110)

-7.5 (-110)

+7.5 (+110)

-7.5 (+110)

 

I was thinking the the split function might be able to get close, but not quite sure how to make that work because I don't know how to make it split at a specific delimiter...

 

Thanks guys..

Link to comment
Share on other sites

As akitchin said, the example seems pretty consistent (plus/minus followed by 110). If that's always the case then some simple string manipulation might suffice.  If not, and something like a regular expression is necessary then the following example snippet takes your input string and outputs what you're shooting for.

 

$subject = '+7.5-110
-7.5-110
+7.5+110
-7.5+110';

echo preg_replace('/[+-]\d+$/m', ' ($0)', $subject);

Link to comment
Share on other sites

Well it could be any of these:

 

+7.5-110

-13.5-110

+24.5+310

-9.5+550

+7-1000

-7+5000

 

where as I'd want:

 

+7.5-110    = +7.5 (-110)

-13.5-110    = -13.5 (-110)

+24.5+310  = +24.5 (+310)

-9.5+550    = -9.5 (+550)

+7-1000      = +7 (-1000)

-7+5000      = -7 (+5000)

 

The numbers and symbols are never consistent per game (all games have different odds).. they are only consistent as to the format of the original string..

 

Am gonna try out salathe's suggestion quick..

Link to comment
Share on other sites

Awesome! As far as I can see it's working perfectly! Thank you salathe..

 

You got one more in ya?? hah

 

Say I got these couple of possible strings:

 

o134.5-110

u134.5-110

o9.5-310

u9.5+230

 

By using something similar to what you just did, how could I make those like so (need 2 ways):

 

1st way:

o134.5-110 = 134.5

u134.5-110 = 134.5

o9.5-310    = 9.5

u9.5+230    = 9.5

 

2nd way:

o134.5-110 = o (-110)

u134.5-110 = u (-110)

o9.5-310    = o (-310)

u9.5+230    = u (+230)

 

Thank you so very much in advanced!!

 

 

 

Link to comment
Share on other sites

Sure, but make sure you really understand what is going on.

 

 

$subject = 'o134.5-110
u134.5-110
o9.5-310
u9.5+230';

// Same pattern for both ways
$pattern = '/^([ou])(\d+(?:\.\d)?)([+-]\d+)$/m';

// Way 1
echo preg_replace($pattern, '$2', $subject);

// Way 2
echo preg_replace($pattern, '$1 ($3)', $subject);

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.