Announcement

Collapse
No announcement yet.

[Function] Currency_Format

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Function] Currency_Format

    PHP Code:
      function currency_format($amount$currency='') {
        
    $currency_array = array(
            
    'GBP' => ''
            
    'JPY' => ''
            
    'CNY' => ''
            
    'EUR' => ''
            
    'USD' => '$',
            
    '' => ''
        
    );
          if ( (
    ctype_alpha($currency) OR empty($currency)) && array_key_exists($currency$currency_array) && ctype_digit($amount) ) {
            
    $ret_txt '<span style="font-weight: bold;">'.$currency_array[$currency].'</span>'.number_format($amount);
          } else {
            
    $ret_txt FALSE;
          }
        return 
    $ret_txt;
      }
        
    $var '2001';
    echo 
    currency_format($var'USD'); // outputs $2,001
    echo currency_format($var'GBP'); // outputs 2,001
    echo currency_format($var'JPY'); // outputs 2,001
    echo currency_format($var'CNY'); // outputs 2,001
    echo currency_format($var'EUR'); // outputs 2,001
    echo currency_format($var); // outputs 2,001
    echo currency_format($var'lol'); // fails
    echo currency_format('+'.$var'USD'); // fails 
    I was bored and needed something like this so i made it and thought i'd post my result

  • #2
    An even nicer function could be exchange rates, since All the currencies have different rates. It'd be cool if you could grab each one from a site that updates them, but current ones are fine

    Comment


    • #3
      I actually was thinking about doing a exchange rate one but the exchange rates are updated all the time there's not much point really.

      Comment


      • #4
        You know any sites with API's that you can grab the current exchange rate from?

        Comment


        • #5
          PHP Code:
          function exchangeRate$amount$currency$exchangeIn )
          {
              
          $googleQuery $amount ' ' $currency ' in ' $exchangeIn;
              
          $googleQuery urlEncode$googleQuery );
              
          $askGoogle file_get_contents'http://www.google.com/search?q=' $googleQuery );
              
          $askGoogle strip_tags$askGoogle );
              
          $matches = array();
              
          preg_match'/= (([0-9]|\.|,|\ )*)/'$askGoogle$matches );
              return 
          $matches[1] ? $matches[1] : false;
          }
           
          echo 
          exchangeRate1000'euro''dollars' ); 
          This what you mean?
          [progressbar=90]Perfecting my Insomnia[/progressbar]

          Comment


          • #6
            never thought of it that way, thanks for that LordDan

            Comment


            • #7
              i made a function like this its in the mccodes free mods it has the conversion rate too

              wops didnt see the auto updating

              Comment


              • #8
                Why not do it a nice simple way? Using a regularly updated XML file with the latest exchange rates, they aren't that hard to find.
                The best way to make your dreams come true is to wake up.
                - Paul Valery

                Facebook Me? | devianWATCH Me? | Tumblr Me? | Tweet Me?

                Comment


                • #9
                  Because I didn't write it, I just did a search and posted as reference ^^ I've never needed such a function myself..
                  [progressbar=90]Perfecting my Insomnia[/progressbar]

                  Comment


                  • #10
                    Oh I see, but the idea is there now, in case someone wishes to do it the way I said.
                    The best way to make your dreams come true is to wake up.
                    - Paul Valery

                    Facebook Me? | devianWATCH Me? | Tumblr Me? | Tweet Me?

                    Comment

                    Working...
                    X