SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Place symbol in front of first integer in variable

    Hi,

    Is there an easy way to append a symbol in front of an integer.

    So if the user types in:

    Offers over 235,000

    then my function will change this to
    Offers over 235,000


    Any ideas?



    Many thanks

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    *slap*

    See money_format.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aaaah.

    So instead of the symbol, it would just use GBP?

    I think I may be a bit cheeky and just use 2 columns in the database to store 'Offers over' and '235,000'


    Thanks again Anthony

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You could add more control by writing your own function, this is very messy but should get the point across.

    PHP Code:
    <?php

    /***
     * @desc  Yeah, money-ize. I'm hip.
     */
    function moneyize($string$prefix '&pound;'){
      return 
    preg_replace_callback(
        
    '~(\d+)~',
        
    create_function(
          
    '$num',
          
    sprintf(
            
    'return "%s" . number_format($num[0], 2);',
            
    $prefix
          
    )
        ),
        
    $string
      
    );
    }

    echo 
    moneyize('Offers above 250000');
    #Offers above &pound;250,000.00

    echo moneyize('Offers above 250000''$');
    #Offers above $250,000.00

    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Anthony!!! That looks pretty good. I'm hoping to play about with it shortly, see how I go.

    Thanks again


Bookmarks

Posting Permissions

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