I need help with splitting a string

Discussion in 'PHP' started by countrydj, Dec 14, 2010.

  1. #1
    I have a script that I want to modify by taking the first letter out of it, adding a figure to the remainder and then adding the first letter back in.

    This probably sounds like double dutch so here it is in more understandable english:

    I want to remove the first character out of this string $string . This string varies in value but the first character is always £ .
    I want to remove the £ and leave the remaining value e.g. 20.00 or 1000.00.

    I then want to add a figure to it.
    e.g. say 10.00 add 1.50 to equal 11.50

    I then want to add the £ back in to finish up with £11.50

    Any help would be appreciated.

    John C
     
    countrydj, Dec 14, 2010 IP
  2. fr33lanc3

    fr33lanc3 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try something like this...

    $string = '$20.00';

    $chop = intval(substr($string, 1)); // $chop now is 20.00
    $add = 10;

    $total = $chop + $add; // total now is 30

    $finalValue = '$' .$total; // should be $30.00
     
    fr33lanc3, Dec 14, 2010 IP
  3. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #3
    you should use floatval() not intval()
     
    shofstetter, Dec 14, 2010 IP
  4. countrydj

    countrydj Active Member

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #4
    Hi Guys...
    Many thanks for your help.
    However, I have played about with this code, using both floatval() and intval(), but the final result drops the final 0's
    I have also tried strval() with the same results.
    i.e.
    If the final value of $chop + $add; is £30.00 it is displayed as £30 (without the final 2 00's)
    If the final value of $chop + $add; is £30.50 it is displayed as £30.5 (without the final 0)
    If the final value of $chop + $add; is £30.95 it is displayed as £30.95 (which is what I want)

    This project is to add into a shopping cart that has no facility for adding carriage to the final total so it is important that I get the final result to 2 decimal places.

    Any other thoughts will be appreciated.

    Regards,

    John C
     
    countrydj, Dec 15, 2010 IP
  5. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #5
    shofstetter, Dec 15, 2010 IP
  6. countrydj

    countrydj Active Member

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #6
    Hi shofstetter..

    Thanks for your help.
    I couldn't get money_format() to work as I needed it.
    However, whilst trying I came across number_format() which worked a trreat for me.

    Thanks for your help.

    John C
     
    countrydj, Dec 15, 2010 IP
  7. backlinkneeded

    backlinkneeded Member

    Messages:
    285
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #7
    I tried that. It works for sure. Thanks all for the good tip.
     
    backlinkneeded, Dec 22, 2010 IP
  8. CyberSorcerer

    CyberSorcerer Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You can also do:

    
    $amount = '£20.00';
    
    echo str_replace('£', '', $amount);
    
    // or
    
    echo str_replace('£', '', $amount);   // if reading string from a url
    
    PHP:
     
    CyberSorcerer, Dec 22, 2010 IP