Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts

    How would you extract this numeric data ?

    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


    .
    Last edited by jeddi; 02-22-2010 at 04:59 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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.

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    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



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by jeddi View Post
    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 ?
    Yup. you need the preg function for extracting the data. I'd try the following regex for both, first.

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

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    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.

  • #6
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks for the input.

    I'll give it a bash and see if it cracks
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #7
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hi matt,

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

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

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



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by jeddi View Post
    Hi matt,

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

    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.

  • #9
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    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

    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 )


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #10
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,412
    Thanks
    13
    Thanked 357 Times in 353 Posts
    well, hyphens in classes ain’t hyphens but define a range, therefore the escaping, sigh.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #11
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Dormilich View Post
    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. Whomever designed regex had a twisted mind.

  • #12
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,412
    Thanks
    13
    Thanked 357 Times in 353 Posts
    Quote Originally Posted by MattF View Post
    Whomever designed regex had a twisted mind.
    I guess he was only lazy.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #13
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hi,
    I have moved forward a bit

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

    PHP Code:
    // 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 ?

    PHP Code:
    // 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?

    PHP Code:
    // 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.
    .
    Last edited by jeddi; 02-23-2010 at 07:22 AM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •