...

View Full Version : How would you extract this numeric data ?



jeddi
02-22-2010, 05:50 PM
Hi,

I have a couple of columns that ı really want to store
as numeric but I am getting it in as a text string.

For example:

$comm = "50.00% on Level1"

and

$price = "$4.99 now, then after 3 days $99.90 every 1 month"

With the first one, I just want to strip out
the "50.00" from "50.00% on Level1"

$comm = 50.00

From what I can see, there is no level2 anyway!


and on the price I think I had better
strip out:

$price = 4.99
$rebill = 99.90

Should I be using regular expressions fro these or is there a better way ?


PS - I just checked the formats and it seems that
for the price $$.cc now is a standard expression

and :

$$.cc every 1 month
$$.cc every 3 months
$$.cc every 6 months
$$.cc every 12 months

are standard expressions.


Thanks


.

MattF
02-22-2010, 06:05 PM
The same answer, (and just a slight variation of regex), to the answer you usually receive for this type of question. Just adapt one of the regex you already have to extract the part you want.

jeddi
02-22-2010, 06:59 PM
haha -
The trouble with you yorkshire lads is that you
always beat around the bush ...



What I meant was, seeing that regex take more
cpu power than say str_replace() does, I was
just trying to make sure I don't use a sledgehammer to crack a nut.

so... the sledgehammer it is then ?

for the "on Level1" I guess that I need the "look behind" ?

(I don't think that is a "slight variation of regex" ! )

so if $comm = "50.00% on Level1"

preg_match_all('/(?< \d+) Level1/', $comm, $matches)

Will that find all these comm rates ? 60.00, 50.00, 25.00

Hope so :)

Now

$price = "$4.99 now, then after 3 days $99.90 every 1 month"

I think I'll have tho find the:
"now" with "look behind"
"then after" with "look ahead" -- find the x days
"every" with "look behind" -- find the 1 or 3 or 6 or 9 or 12


Am I on theright track ?

Or would str_replace() be better ?

Thanks for any input



.

MattF
02-22-2010, 07:06 PM
haha -
The trouble with you yorkshire lads is that you
always beat around the bush ...


:D :D



What I meant was, seeing that regex take more
cpu power than say str_replace() does, I was
just trying to make sure I don't use a sledgehammer to crack a nut.

so... the sledgehammer it is then ?

Yup. you need the preg function for extracting the data. I'd try the following regex for both, first.



preg_match_all('#([\.\-\d]+)#', $comm, $matches)

Inigoesdr
02-22-2010, 07:13 PM
If there is a chance that your match will ever be at a different location in the string(ie. the offset, or the string content might change), OR if you need a pattern-based match it is better to just use a regular expression from the start. Also, while the regex engine does take extra resources, it's much more powerful and you will likely never notice the difference in resources anyway.

jeddi
02-22-2010, 08:20 PM
Thanks for the input.

I'll give it a bash and see if it cracks :)

jeddi
02-22-2010, 09:25 PM
Hi matt,

I have just been reading up up regexs and I still
can not see what your suggestion is doing :confused:

preg_match_all('#([\.\-\d]+)#', $comm, $matches)

Obviously the \d I know cos I used it but what is the
\.\- doing ?



.

MattF
02-22-2010, 09:30 PM
Hi matt,

I have just been reading up up regexs and I still
can not see what your suggestion is doing :confused:

preg_match_all('#([\.\-\d]+)#', $comm, $matches)

Obviously the \d I know cos I used it but what is the
\.\- doing ?

Matching dots and hyphens. £29.99, £29-99. Both are valid formats. They're just escaped because I have a shonky memory when it comes to remembering which are reserved characters in regex. :D

jeddi
02-22-2010, 10:04 PM
OKay

Thanks for sharing ....
actually I was going mad trying to find out what the hyphen as a
special character meant. When all along you meant a ... hyphen :D

Thanks again.

... I might need a little more help with the next bit
but I am off to bed now ( I am 2 hours ahead of Yorkshire )


.

Dormilich
02-22-2010, 10:19 PM
well, hyphens in classes ain’t hyphens but define a range, therefore the escaping, sigh.

MattF
02-22-2010, 11:15 PM
well, hyphens in classes ainít hyphens but define a range, therefore the escaping, sigh.

Unless they're the first character in a range, of course. :D Whomever designed regex had a twisted mind. :D

Dormilich
02-23-2010, 07:37 AM
Whomever designed regex had a twisted mind. :D

I guess he was only lazy.

jeddi
02-23-2010, 08:16 AM
Hi,
I have moved forward a bit :)

For the Commission:
Format: $commission = "50.00% on Level1"


// Calculate commission
if(preg_match('#([\.\-\d]+)#', $commission, $matches) == 1) {
$comm = $matches[0];
}
else {
$comm = 0;
}


For the Prices, it is a little more complicated:

Here are two examples of the format:

$price = "$79.00 now, then after 1 month $4.90 every 1 month"
$price = "$5.95 now, then after 14 days $37.00 every 30 days for 6 times"


Here I am trying to do a "look behind" but I don't know if I have the
syntax right ?


// Calculate prices - initial
if(preg_match('#([?< \d]+) now,#', $price, $matches) == 1) {
$price_init = $matches[0];
}
else {
$price_init = 0;
}


The following is supposed to be a "look ahead" but again, possibly I have the
syntax wrong?



// Calculate prices - gap
if(preg_match('#([?=\d]+) then after#', $price, $matches) == 1) {
$price_gap = $matches[0];
}
else {
$price_gap = 0;
}


// Calculate prices - gap unit

Now I need to find out if the unit is "days" or "month(s)".
The unit comes after the number (which comes after "then after").

What would you recommend ?

PS
Then I will try and get the
$price_frequency, $price_period and $ price_duration.
.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum